|
From: Saundra S. <Sch...@ho...> - 2000-11-11 15:01:42
|
Hi, I've been having difficulty getting my switch statements to work =
properly. When I run the program no matter what I enter I get the =
default response. Please help. I've inculded a sample from my text. I =
need the switch statement to work for an assignment.
#include <stdlib.h>
int main()
{
int grade;
int aCount =3D 0, bCount =3D 0;
printf("Enter the letter grades.\n");
printf("Enter the -1 to end input.\n");
while( (grade =3D getchar()) !=3D -1)
{
switch(grade)
{
case 'a': case 'A':
++aCount;
break;
case 'b': case 'B':
++bCount;
break;
default:
printf("Incorrect letter grade entered.");
printf("Enter a new grade.\n");
break;
}
}
printf("%d\n", aCount);
printf("%d\n", bCount);
system("PAUSE");
return 0;
}
|
|
From: Noorez K. <coo...@ho...> - 2004-05-29 05:34:20
|
I am having a problem with the case statement with both the switch
statements in the program.
They are both exactly the same really.... I marked the lines with errors
The compile errors are
[Warning] Infunction 'int main(int, char**)
Line 57/110 Jump to case label
Line 50/103 crosses initialization of 'char intern[16]';
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main(int args, char* pszArgs[])//there needs to be args for this to work
{
if(strcmp(pszArgs[1],"-nt")== 0)
{
system("cls");//start with a clear screen
//explain what the heck all this does
cout << "Welcome to the easy program opener!\n";
cout << "Hear is a list of programs that can be easy opened\n\n";
cout << "To quit enter 0 or nothing for a program choice\n";
cout << "1. Microsoft Office\n";
cout << "2. Microsoft Paint\n";
cout << "3. Calculator\n";
cout << "4. Internet Explorer\n";
int choice;
while(1)
{//while
choice = 0;
cout << "\nEnter the number of your choice: ";
cin >> choice;
switch(choice)
{//switch
case 0:
cout << "Exiting easy opener...\n";
//Sleep(1000);
return 0;
break;
case 1:
system("start winword");
break;
case 2:
system("mspaint");
break;
case 3:
system("calc");
break;
case 4:
char site[123];
char intern[] ="start iexplore "; /*50*/
cout << "Enter site: ";
cin >> site;
strcat(intern,site);
system(intern);
break;
default://the choice wasn't a valid one if we are at default /*line
57*/
cout << "Not a valid choice\n";
}//switch
}//end while
}//done defining nt operating system
if(strcmp(pszArgs[1],"-me")==0)
{
system("cls");
cout << "Welcome to the easy program opener!\n";
cout << "Hear is a list of programs that can be easy opened\n\n";
cout << "To quit enter 0 or nothing for a program choice\n";
cout << "1. Microsoft Office\n";
cout << "2. Microsoft Paint\n";
cout << "3. Calculator\n";
cout << "4. Internet Explorer\n";
int choice;
while(1)
{
cout << endl << "Enter choice: ";
cin >> choice;
switch(choice)
{
case 0:
cout << "Exiting easy opener...\n";
//Sleep(1000);
return 0;
break;
case 1:
system("start winword");
break;
case 2:
system("pbrush");
break;
case 3:
system("calc");
break;
case 4:
char site[123];
char intern[] ="start iexplore "; /*103*/
cout << "Enter site: ";
cin >> site;
strcat(intern,site);
system(intern);
break;
default: /*line 110*/
cout << "Not a valid choice\n";
}//end switch
}//end while
}//end defining me operating system
if(strcmp(pszArgs[1],NULL)==0 )//opps! nothing found!
{
cout << "This program requries the use of Args\n"
<< "Use the name of your operating system\n"
<< "eg... -me for Millenium Edition\n\n";
cout << "Open a command prompt window and type pszArgs[0] -<short name
of system>";
cout << "Just don't use the << ";
system("pause");
return 1;
}
return 0;//exit program
}
_________________________________________________________________
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
|
|
From: Per W. <pw...@ia...> - 2004-05-29 06:36:53
|
Don't declare variables within the switch statement without using braces,
to create a separate namespace for the variable!
switch (n) {
case 0:
{
int local_variable;
break;
}
default:
// because of braces, the local_variable isn't visible anymore.
}
/Per W
On Fri, 28 May 2004, Noorez Kassam wrote:
> I am having a problem with the case statement with both the switch
> statements in the program.
> They are both exactly the same really.... I marked the lines with errors
>
> The compile errors are
> [Warning] Infunction 'int main(int, char**)
> Line 57/110 Jump to case label
> Line 50/103 crosses initialization of 'char intern[16]';
>
> #include <iostream>
> #include <cstdlib>
> #include <string>
> using namespace std;
>
> int main(int args, char* pszArgs[])//there needs to be args for this to work
> {
> if(strcmp(pszArgs[1],"-nt")== 0)
> {
> system("cls");//start with a clear screen
> //explain what the heck all this does
> cout << "Welcome to the easy program opener!\n";
> cout << "Hear is a list of programs that can be easy opened\n\n";
> cout << "To quit enter 0 or nothing for a program choice\n";
>
> cout << "1. Microsoft Office\n";
> cout << "2. Microsoft Paint\n";
> cout << "3. Calculator\n";
> cout << "4. Internet Explorer\n";
>
> int choice;
> while(1)
> {//while
>
> choice = 0;
> cout << "\nEnter the number of your choice: ";
> cin >> choice;
> switch(choice)
> {//switch
> case 0:
> cout << "Exiting easy opener...\n";
> //Sleep(1000);
> return 0;
> break;
>
> case 1:
> system("start winword");
> break;
>
> case 2:
> system("mspaint");
> break;
>
> case 3:
> system("calc");
> break;
>
> case 4:
> char site[123];
> char intern[] ="start iexplore "; /*50*/
> cout << "Enter site: ";
> cin >> site;
> strcat(intern,site);
> system(intern);
> break;
>
> default://the choice wasn't a valid one if we are at default /*line
> 57*/
> cout << "Not a valid choice\n";
> }//switch
>
> }//end while
> }//done defining nt operating system
>
> if(strcmp(pszArgs[1],"-me")==0)
> {
> system("cls");
> cout << "Welcome to the easy program opener!\n";
> cout << "Hear is a list of programs that can be easy opened\n\n";
> cout << "To quit enter 0 or nothing for a program choice\n";
>
> cout << "1. Microsoft Office\n";
> cout << "2. Microsoft Paint\n";
> cout << "3. Calculator\n";
> cout << "4. Internet Explorer\n";
>
> int choice;
> while(1)
> {
> cout << endl << "Enter choice: ";
> cin >> choice;
> switch(choice)
> {
> case 0:
> cout << "Exiting easy opener...\n";
> //Sleep(1000);
> return 0;
> break;
>
> case 1:
> system("start winword");
> break;
>
> case 2:
> system("pbrush");
> break;
>
> case 3:
> system("calc");
> break;
>
> case 4:
> char site[123];
> char intern[] ="start iexplore "; /*103*/
> cout << "Enter site: ";
> cin >> site;
> strcat(intern,site);
> system(intern);
> break;
>
> default: /*line 110*/
> cout << "Not a valid choice\n";
> }//end switch
>
> }//end while
>
> }//end defining me operating system
>
> if(strcmp(pszArgs[1],NULL)==0 )//opps! nothing found!
> {
> cout << "This program requries the use of Args\n"
> << "Use the name of your operating system\n"
> << "eg... -me for Millenium Edition\n\n";
>
> cout << "Open a command prompt window and type pszArgs[0] -<short name
> of system>";
> cout << "Just don't use the << ";
>
> system("pause");
> return 1;
> }
>
> return 0;//exit program
> }
>
> _________________________________________________________________
> http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: Oracle 10g
> Get certified on the hottest thing ever to hit the market... Oracle 10g.
> Take an Oracle 10g class now, and we'll give you the exam FREE.
> http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ya...> - 2000-11-11 18:32:26
|
At first, you did not include <stdio.h>. Second, the ==-1 also does not work
since getchar reads a character a time and the -1 of the user input is not
read together. Now the most important, the program reads the character, the
error message is for the '\n' character which the next getchar() reads after
your input. So take a look at the improved code:
#include <stdio.h> /* You forgot it */
#include <stdlib.h>
int main()
{
int grade;
int aCount = 0, bCount = 0;
printf("Enter the letter grades.\n");
printf("Enter the Q to end input.\n");
while( (grade = getchar()) != 'Q') /* 'Q' instead of '-' '1' */
{
getchar(); /* It absorbs the next '\n' */
switch(grade)
{
case 'a': case 'A':
++aCount;
break;
case 'b': case 'B':
++bCount;
break;
default:
printf("Incorrect letter grade entered.");
printf("Enter a new grade.\n");
break;
}
}
printf("%d\n", aCount);
printf("%d\n", bCount);
system("PAUSE");
return 0;
}
|
|
From: lstar36 <ls...@fl...> - 2000-11-12 02:54:03
|
The function getchar() is a macro which leaves a char in the buffer so =
the second time throught the while loop you get the error print. Use =
getche(). Also it is better programming to conver the keyboard input =
to an upper or lower case with the function toupper() and then use only =
a single case. Also use a do{ while()} loop to make the program easier =
for someone else to understand.
example:
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
int grade;
int aCount =3D 0, bCount =3D 0;
printf("Enter the letter grades.\n");
printf("Enter the letter x to end input.\n");
do
{
grade =3D toupper(getche());
switch(grade)
{
case 'A':
++aCount;
break;
case 'B':
++bCount;
break;
case 'X':
printf("\nend of input\n");
break;
default:
printf("\nIncorrect letter grade entered.\n");
printf("Enter a new grade.\n");
break;
}
}
while (grade !=3D 'X');
printf("%d\n", aCount);
printf("%d\n", bCount);
system("PAUSE");
return 0;
}----- Original Message -----=20
From: Saundra Schwarz=20
To: dev...@li...=20
Sent: Saturday, November 11, 2000 12:01 PM
Subject: [Dev-C++] switch statement
Hi, I've been having difficulty getting my switch statements to work =
properly. When I run the program no matter what I enter I get the =
default response. Please help. I've inculded a sample from my text. I =
need the switch statement to work for an assignment.
#include <stdlib.h>
int main()
{
int grade;
int aCount =3D 0, bCount =3D 0;
printf("Enter the letter grades.\n");
printf("Enter the -1 to end input.\n");
while( (grade =3D getchar()) !=3D -1)
{
switch(grade)
{
case 'a': case 'A':
++aCount;
break;
case 'b': case 'B':
++bCount;
break;
default:
printf("Incorrect letter grade entered.");
printf("Enter a new grade.\n");
break;
}
}
printf("%d\n", aCount);
printf("%d\n", bCount);
system("PAUSE");
return 0;
}
|