while loop and switch statement

numbers, sum, switch statement

Page 1 of 1

3 Replies - 5277 Views - Last Post: 09 November 2010 - 10:53 PM Rate Topic: -----

#1 lobabyg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-October 10

while loop and switch statement

Posted 08 November 2010 - 08:35 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: while loop and switch statement

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

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
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

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:

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.
Was This Post Helpful? 0
  • +
  • -

#4 lobabyg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-October 10

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






#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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1