Program Restart and Name with Space

I have a problem and I can't seem to find a good solution.
I'm just beginning in programming but I had this two problems where I can't solve.

The problem:
1. This program has 4 options, one of the requirements that I must do is, IF I enter a value that is not on the choices the program should restart. However I don't know how.

2. In the average part, I need to enter a name, however IF I use a name with space while running the program, the program would end.

Can anyone help me on this? (Using Dev C++)
I removed the 3 options part because it's working fine.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<string>

using namespace std;
int main()
{    
    int ans;
    cout<<"MAIN MENU:"<<endl<<endl;
    cout<<"[3] Find the Average."<<endl<<endl;
    cout<<"Enter a number of your choice: ";
    cin>>ans;

     switch (ans)
     
     case 3: //Average section
     {
          system("cls");
          int num,i;
          float g2,ave,grade,total;
          char f [20];
          char l [20];
          cout<<"Enter the name:"<<endl;
          cin<<f;
          cout<<"Enter the name: "<<endl;
          cin<<l;
          cout<<"Enter the number of grades which will be added: "<<endl;
          cin>>num;
          
          g2=0;
          i=0;
          total=0;
          system("cls");
                    
          while (i<num) //loop
          {         
          cout<<"Enter a grade: "<<endl;
          cin>>grade;
          total=total+grade;
          i++;          
          }
           
          ave=(total)/i;
          cout<<f<<endl;
          cout<<"The total average of the grades is: "<<ave<<endl;
          system("pause");
          break;
          }
          default:
                  {
            cout<<"You have entered an invalid value. Please restart the program.";
            system("pause");
                   }          
          }
          }
          


Last edited on
Instead of using char use strings maybe? I'm not sure if spaces are even picked up by char.
I don't know much about how to restart the program, you can use system(char*) of <stdio.h> but people say it wrong to do so (there is an article on this website in the articles section which lists out the reasons).

There are other ways using Windows API which I know of. I'll only give the lengthy source code if you are interested and have windows.

cin>> interprets any type of whitespace and the Enter key as seperators. It will take input only till it encounters a space or an enter.

What happens is that when you hit enter, cin stores everything you entered in a memory location called the input buffer. Then it starts reading the input buffer and stops when it encounters '\n', or any whitespace character (like space). It translates the input into a double, int or any other data type if you are using cin>> with that data type. If it is a string, it just copies what it has read into your string. It then deletes the part that it has just read.

If you didn't enter a whitespace, it will delete everything except the '\n'.

Otherwise, it will delete everything before the first whitespace and everything else will remain in the output buffer. When you use cin the next time, instead of taking input from you, it will continue reading leftover characters from the input buffer.

So a possible workaround is to use cin.getline(char* a,int size,char delim='\n'). For eg, if you want to take input into a c-string, this will work.
1
2
char a[30];
cin.getline(a,30);


This will take input till it encounters a '\n' or it has read 30 characters.

But there is a catch. getline will remove the '\n' from the input buffer, cin>> wouldn't. cin removes it before reading.
As cin leaves behind a '\n', using getline after cin will cause getline to read the '\n', remove it from the input buffer and then stop. It wouldn't read after that.

So to be able to use getline after a cin, you have to check the first character in the input buffer and remove it if it is a '\n'. This is done like this:
if(cin.peek()=='\n')cin.ignore();
Just after this, you can use getline

For more information on these functions, you can refer to the Reference section of this website. Look in the IOstream library under istream class
I see, thanks! Got it working now.
Topic archived. No new replies allowed.