how do i return to the program

the compiler i am using is Dev-C++, by bloodshed. i think.

my question is how do i return to the loop to redo the program..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

int main()

{
    int num=0;
    
    while(1)
    {
            cout<<"Enter a number to be cubed and then squared.\n";
            cout<<"The number needs to be between 1 and 10\n";
            while(1)
            {
                    cout<<"Number is: ";
                    cin>>num;
                    if(num>=1 && num<=10)
                              break;
                    else
                        cout<<"Wrong number try again\n";
            }//closing nested while
            cout<<"\n\n";
            cout<<num<<" squared is "<<pow(num,2);
            cout<<"\n";
            cout<<num<<" cubed is "<<pow(num,3);
            int brk=0;
            cout<<"\n\nrun again? 1=yes,2=no ";
            cin>>brk;
            if(brk==2);  
                  break;
            
                  
    }//closing 1st while brace
     
                  
    return(0);
}


if there is another way to to do this, please tell me. and this is in WINDOWS XP if that helps.
everytime I execute it, it will go. but if i want to repeat the code it will not go again.

Thanks in advance.

You accidentally left a semicolon (;) at the end of line 32, separating the if and the break into two statements.

1 if (brk==2) /* do nothing */ ;
2 break;

It's often the silly stuff that gets ya, no? :-)
Last edited on
alright I will fix that and get back to this.

ok it works, thanks for that..

I should have noticed that little error.. making one statement into two
Last edited on
along with this.. what can I enter into my code to make so that if the user enters a letter, or something besides a number, so that it will not turn into an infinite loop?
Check for !cin.good(), then call cin.clear() if it is in that state.
where in the code do i put that?
after the cin>>brk;
Just check !cin.good (or just !cin ?) and if is true, then call cin.clear() and print out your error msg.
i am sorry, i am quitely new to this. and by putting in !cin.good after the cin>>brk; ... i receive an error message.

38 G:\Dev-Cpp\main.cpp could not convert `std::cin.std::basic_ios<_CharT, _Traits>::good [with _CharT = char, _Traits = std::char_traits<char>]' to `bool'


what does that error mean?

and then...
38 G:\Dev-Cpp\main.cpp in argument to unary !

and
38 G:\Dev-Cpp\main.cpp expected `;' before '}' token



this is what i have..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
            int brk=0;
            cout<<"\n\nrun again? 1=yes,2=no ";
            cin>>brk;
            if(brk==2) 
                  break;
            !cin.good
                  
    }//closing 1st while brace
     
  
   cin.ignore(1);
             
    return(0);
}
Ah...yeah ^^; Put it in an if statement:

1
2
3
4
5
6
if(brk==2) 
   break;
if(!cin.good) {
   cin.clear(); //clear errors/stuff
   //do error handling (i.e. cout << "INVALID OR CORRUPT INPUT";
}


EDIT: Btw, that means that it can't convert "cin.good" to a bool, and the second error means the ! (which needs a bool to the right) can't do it's operation (because cin.good isn't a bool). The final error means that it thinks you needed a ; at the end of the line because it thought it was a normal expression.

FYI: cin.good != cin.good()
Last edited on
you could do this
1
2
3
4
5
6
7
8
  while (x != 0) {

code.... blah blah


cout <<"\nPress 0 to exit, anything else to continue  : ";
cin x;
}
Last edited on
@ firedraco

Do I need another library to use the
1
2
3
4
5
6
if(brk==2) 
   break;
if(!cin.good) {
   cin.clear(); //clear errors/stuff
   //do error handling (i.e. cout << "INVALID OR CORRUPT INPUT";
}


??

@ spy4hearts

i am trying to make the program so if i enter in a character such as "a", it will not go into an infinite loop repeating my error message "Wrong Number try again"

---------------------------

would this work?
 
if(brk !=1 || brk !=2)


if I do that , i'm not sure what to do next.

Suggestions?
Last edited on
!cin.good needs to be !cin.good()...other then that, you *could* do that, but if someone enters a letter then your program will loop and error.
so there is no decent way to make a program like that "idiot" proof?
Yeah...the way I suggested, except make it a while loop:

1
2
3
4
5
6
7
8
9
10
int brk=0;
cout<<"\n\nrun again? 1=yes,2=no ";
cin>>brk;
while(!cin.good()) {
   cin.clear();
   cout<<endl<<"INVALID OR CORRUPT INPUT";
   cout<<"\n\nrun again? 1=yes,2=no ";
   cin>>brk;
}
if(brk==2) break; //ifs don't need semi colons 
Topic archived. No new replies allowed.