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
}

Recommended Answers

All 3 Replies

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?

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.