| View previous topic :: View next topic |
| Author |
Message |
reubatsch456 Cheater
Reputation: 0
Joined: 03 Jan 2008 Posts: 30
|
Posted: Sun Jan 27, 2008 12:18 pm Post subject: Several C questions. |
|
|
I'd appreciate it if you guys can help me answer these questions.
-When using a switch statement, how can I display an error message if the person enters in a decimal value or a character? I have this code and every time I enter in a char or decimal value instead of an int, it just goes crazy.
| Code: | void menu(void)
{
int menu_input = 0;
do
{
puts("\n1 - Instructions for Use");
puts("2 - Start Calculating");
puts("3 - Quit\n");
printf("Your choice: ");
scanf("%d", &menu_input);
switch (menu_input)
{
case 1:
{
instruction();
break;
}
case 2:
{
calculator();
break;
}
case 3:
{
;
break;
}
default:
{
printf("The value you entered is invalid. Try again.\n");
break;
}
}
}
while (menu_input < 1 || menu_input > 3);
} |
-Also, is there a way to display a double without having it be in scientific notation?
I'm using dev c++ as a compiler so u guys know.
-thank you very much.....if u want me to further clarify my problems, just leave a post and ill get back to it asap.
_________________
|
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Sun Jan 27, 2008 1:18 pm Post subject: |
|
|
For 1st question, use an if statement in the default, if you want your error message to be specific. Like so:
| Code: |
default:
{
if(menu_input !<=> 0)
printf("You must enter an integer!\n");
else
printf("The value you entered is invalid. Try again.\n");
break;
}
|
I don't know how to find out if it is a decimal value, but just round it, which will work better. This way it won't matter.
| Code: |
float menu_input
scanf("%f", &menu_input);
//After all calculations done:
printf("The answer is: %d", menu_input);
|
Also, you don't need a break in the default, as it will automatically go to the next thing down, in your case, the while loop.
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Jan 27, 2008 2:06 pm Post subject: |
|
|
Wrote my own little example too with the updated versions (secure versions) of scanf and such as well as using the TCHAR form.
| Code: | #include <windows.h>
#include <iostream>
#include <tchar.h>
int MenuCheck()
{
TCHAR tszInput[255] = {0}; // Holds Input String
double dbConvOption = 0; // String Converted To Double
// Display Options
_tprintf_s( TEXT("Please enter an option from the menu below.\r\n") );
_tprintf_s( TEXT("1 - Instructions For Use\r\n") );
_tprintf_s( TEXT("2 - Start Calculating\r\n") );
_tprintf_s( TEXT("3 - Quit\r\n") );
_tprintf_s( TEXT("Your choice: ") );
// Obtain User Input
_tscanf_s( TEXT("%s"), &tszInput, _ARRAYSIZE(tszInput) );
// Convert To Double And Check For Decimal
dbConvOption = atof( tszInput );
for( int i=0;i<_ARRAYSIZE(tszInput);i++ )
{
if( tszInput[i] == '.' )
{
std::cout << TEXT("\r\nDecimal found, please enter a valid option.\r\n");
return -1; // -1 return means invalid option, call MenuCheck again.
}
}
// Convert Double To Int And Check Option (Can just leave as double if you want.)
switch( (int)dbConvOption )
{
case 1:
// Do Instruction Here
return 1;
case 2:
// Do Calculator Here
return 2;
case 3:
// Exit Here
return 3;
default:
return -1; // -1 return means invalid option, call MenuCheck again.
}
// If we got here for any reason, return invalid.
return -1;
}
int main()
{
// Console Buffer Information
CONSOLE_SCREEN_BUFFER_INFO pConsoleBufferInfo;
COORD cCoords = {0};
// Call MenuCheck And Obtain Return
int iMenuReturn = 0;
while( iMenuReturn != 3 )
{
// Clear Console
GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &pConsoleBufferInfo );
FillConsoleOutputCharacter( GetStdHandle(STD_OUTPUT_HANDLE), 32, pConsoleBufferInfo.dwSize.X*pConsoleBufferInfo.dwSize.Y, cCoords, NULL );
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), cCoords );
// Run Menu Check
iMenuReturn = MenuCheck();
// Check For Invalid Option
if( iMenuReturn == -1 )
{
std::cout << TEXT("\r\nYou have entered an invalid option, please press enter and try again.");
std::cin.sync();
std::cin.ignore();
}
}
std::cout << TEXT("\r\n\r\nPress enter key to exit.");
std::cin.sync();
std::cin.ignore();
} |
EDIT: Had a slight mistake, fixed.
_________________
- Retired. |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun Jan 27, 2008 3:05 pm Post subject: |
|
|
Wiccaan, source generally looks better if you use "_T" instead of "TEXT". It does the exact same thing, but is far less ugly.
|
|
| Back to top |
|
 |
the_undead Expert Cheater
Reputation: 1
Joined: 12 Nov 2006 Posts: 235 Location: Johannesburg, South Africa
|
Posted: Sun Jan 27, 2008 3:13 pm Post subject: |
|
|
meh. matter of personal preference really.
I like TEXT.
_________________
|
|
| Back to top |
|
 |
reubatsch456 Cheater
Reputation: 0
Joined: 03 Jan 2008 Posts: 30
|
Posted: Sun Jan 27, 2008 3:58 pm Post subject: |
|
|
Wow thanks, you guys are helpful. What exactly does <=> do? I've never seen it before. And is using the switch statement:
| Code: |
switch (a)
{
case 1:
/statements
case 2:
/statements
case 3:
/statements
default:
/statements
}
|
The same as:
| Code: |
if (a == 1)
/statements
if (a == 2)
/statements
if (a == 3)
/statements
else
/statements
|
Or does using one have performance benefits or something?
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Jan 27, 2008 6:12 pm Post subject: |
|
|
I think that the switch has performance benefits, I read that somewhere, but I'm not too sure.
_________________
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sun Jan 27, 2008 8:10 pm Post subject: |
|
|
| switches use a jump table that skip testing cases that are going to fail giving them O(1) efficiency
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
| Back to top |
|
 |
|