Hi,
I have an assignment that asks me to read numbers from cin, total the numbers, use a switch statement to add another total of numbers under 100 and numbers over 100. I have tried many variations and have taken tutorials. I do not actually see why there is a need for a switch statement. Regardless, I guess I do not fully understand what I am switching but I keep coming up with error "illegal case" illegal break. I wanted to make a really simple code but it always seems to get out of hand. I am two weeks only into c++. I tried to find out about posting the code but when I put the [code] etc it did not bring up any numbers. I guess I have to be asked to post it.
Thank you,
lobabyg
while loop and switch statementnumbers, sum, switch statement
Page 1 of 1
3 Replies - 3425 Views - Last Post: 09 November 2010 - 10:53 PM
Replies To: while loop and switch statement
#2
Re: while loop and switch statement
Posted 08 November 2010 - 08:40 PM
1) your going to be asked to do(even have to do) things that don't make sense, that's just life.
2) how about posting some code so we can see what the error is
2) how about posting some code so we can see what the error is
#3
Re: while loop and switch statement
Posted 09 November 2010 - 04:04 AM
break can only appear within a switch block or a loop block. Outside of those it would be considered illegal. For example:
Can't really help you further with your particular situation without the code.
int main(void)
{
int i = 0;
while (i < 10)
{
if (i == 5)
break; // Legal
}
break; // Illegal, nothing to break out of
switch(i)
{
case 0:
printf("This can't happen!\n");
break; // Legal
case 5:
printf("Breaking out\n");
break; // Legal
default:
printf("Another impossibility\n");
break;
}
break; // Illegal, again, nothing to break out of
return 0;
}
Can't really help you further with your particular situation without the code.
#4
Re: while loop and switch statement
Posted 09 November 2010 - 10:53 PM
Hi,
I no longer have the problem. I have a problem of the program not letting me input more than one number. I have been trying for many hours and tutorials. Any assistance is greatly appreciated. Remember I am two weeks new.
Thank you,
lobabyg
I no longer have the problem. I have a problem of the program not letting me input more than one number. I have been trying for many hours and tutorials. Any assistance is greatly appreciated. Remember I am two weeks new.
Thank you,
lobabyg
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//global Declarations of Variables
int itotal_inputs=0, iunder_hundred=0, iover_hundred=0, itotal_sum = 0;
int itotal;
int inputs;
int counter; ;
int main()
{
iunder_hundred = inputs < 100 ;
iover_hundred = inputs > 100 ;
itotal_sum = (inputs);
cout << "\nEnter a number\n "<< endl;
cin >> inputs;"\n" ;
while (counter > 1)
(counter)+1 ;
+(inputs)+ inputs ;
switch (iunder_hundred)
{
case 1:
"The total sum is under 100:" ;
break;
case 2: "The total sum is over 100:" ;
break;
}
cout << "Total Sum of inputs ............. = " << inputs++ << endl ;
cout << "Total number of inputs............ = " << counter + 1 << endl;
cout << "Total sum is under 100............ = " << iunder_hundred << endl ;
cout << "Total sum is over 100............. = " << iover_hundred << endl;
}
This post has been edited by JackOfAllTrades: 10 November 2010 - 04:18 AM
Reason for edit:: Fixed bad formatting
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|