0

Hi, I'm new to C++ and my first assignment is to convert C code into C++ code.

Here I have a loop, however; I can't seem to get it to break out of the loop. It is suppose to input an odd number that is less than 15 and set the input to the pointer. Any help would be appreciated.

void CMatrix::GetData()
{
     int nInput = 0;

     for(;;) //infinite for loop
     {
            cout << "\nEnter a positive,";
            cout << " odd integer (-1 to exit program):\n";
            cin >> nInput;
            pInput = &nInput;
            
//            cout << "Pointer " << *pInput << "\n" << nInput;
            
            //if numbers entered, enter the block
            if (cin.good()){
               if (nInput == -1){
                  break;
               }
               else if (nInput > 15){
                    cout << "Sorry, but the integer has";
                    cout << " to be less than 15.\n";
               }
               else if (nInput%2 == 0){
                    cout << "Sorry, but the integer has to be odd.\n";
               }
            }//end if cin.good()
            else if (!isdigit(nInput))
                 cout <<"Only digits are allowed!\n";
            
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
     }//end for loop
}
2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by d34dw4rd
0

put a break statement inside all of the if statement. Why not combine all of
the if statement and provide a generic error message, if the detailed
message is not important?

0

I can't just do that because I need it to keep asking until a correct number is imputed, so it should only break after either -1 or a correct number

0

ok I think i got it to work after putting a break on the main if statement however now I get an "Illegal System DLL Relocation" what is that?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.