How do i get the loop to go back and prompt using for a valid number?

#include<iostream>
#include<conio.h>
#include<iomanip>
#include<string>

using namespace std;

int main ()
{    
    string firstName;
    string lastName; 
    int score1, score2, score3, score4, score5;
    float totalpoints;

    cout<<fixed<<showpoint<<setprecision(2);

    cout<<"Please enter the student first and last name: ";
    cin>>firstName>>lastName;
    cout<< endl; 

cout<<"Please enter the student's FIVE program scores"<<endl;
    cout<<"Each score must be an integer between 0 & 100:"<<endl;
    cout<<endl;



    cout<<"Please enter the first program score: ";
    cin>>score1;

    if (score1>100) 
    {
       cout<<"Invalid Number";
       }
    else if (score1<0)
    {
         cout<<"Invalid Number";
         } 
    cout<<endl<<"Press any key to continue."<<endl;
    getch();

 return 0;   
}

Recommended Answers

All 2 Replies

Well, for starters, you need to actually create a loop. You don't have any loop structure in this code. Just remember, everything that you want to execute multiple times needs to be within the statement block controlled by the loop.

void someFunction() {

  //any code here will only run once

  while (someCondition == true) {
    //any code here will repeat as long as someCondition == true
  }                                           //end while

  //any code here will only run once

}                                             //end main()

NOTE: There are also do-while and for loops. This example only shows the standard while loop.

More information.

you can do

while(score1>100 || score1<0) // will loop until proper number entered
{
cout<<"invalid number,enter again\n";
cin>>score1;
}
commented: Don't just give away code. :( -1
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.