0

I need to write a C++ code that will ask the user to enter either 1 or 2. If 1 is entered the program must count all even numbers from 0 to 100. (I got that part done) If 2 is entered you are suppose to prompt the user to enter their first, last, student id, and then print that information back for the user to see, if neither 1 or 2 is chosen, the program is to exit.

My problem is when i select 2, it will only let me enter the first name then it fills out the rest by itself. What did I do wrong?

#include <iostream>

using namespace std;

int main()
{
  int c,x=0;
  float nm1, nm2, sid;
 cout<<"Please enter either the number 1 or the number 2: "<<endl;
 cin>>c;
 
 if (c == 1)
 {
   while (x <= 100)
   {
         cout << x << endl;
         x = x + 2;
         }
    
 }
 else if (c == 2)
 {
     cout<<"Please enter your first name: ";
     cin>>nm1;
     cout<<"Please enter your last name: ";
     cin>>nm2;
     cout<<"Please enter your student id ";
     cin>>sid;
     cout<<"Your name is: "<< nm1<<nm2<< "and your student ID is: "<<sid<<endl;
   }
     else
     cout<<"you fail at following instructions, please try again"<<endl;
     system("pause");
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by stereomatching
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.