String does not want to accept 'white spaces' when I type and will only accept one word

Basically, my only problem with my code is that when it asks for the student name, and I type anything with spaces (ex. Angelina Jolie, Brad Pitt), the program goes wrong. But if I type in only one word with NO spaces at all (ex. Brad, Pitt, Angelina, Jolie), my program works TOTALLY 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
#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <string>


using namespace std;


void main()
{

float gradeArray [5];
string studentName;
float studentAverage;
int i=0;

int count=0;

while ( i <=10)

{
printf("Enter the name of the student: ");
scanf(" %s", studentName);


printf("Enter quiz 1: ");
scanf(" %f", &gradeArray[0]);

printf("Enter quiz 2: ");
scanf(" %f", &gradeArray[1]);

printf("Enter quiz 3: ");
scanf(" %f", &gradeArray[2]);

printf("Enter quiz 4: ");
scanf(" %f", &gradeArray[3]);

printf("Enter quiz 5: ");
scanf(" %f", &gradeArray[4]);

studentAverage= ((gradeArray[0]+ gradeArray[1]+ gradeArray[2] + gradeArray[3] + gradeArray[4]) / (5));
printf("The student's average is: %f \n\n", studentAverage);

i++;
}

getch();

}



Hope anyone could help me cause I've been stressing out for the past few days. ):
Hi, this is normal: the scanf function reads until it finds a blank space. To overcome the 'problem' you can use the fgets function.
Hello! Thanks for your reply :) It works the first time around... cause my code loops ten times, so in the first time you type two or more words in the "name of student", it works. Here's a sample of the output:

Enter the name of the student: Andy Miller
Enter Quiz 1: 10
Enter Quiz 2: 10
Enter Quiz 3: 10
Enter Quiz 4: 10
Enter Quiz 5: 10
The student's average is: 10.000000

That's the result and it works, but once it loops back again (2nd loop) to the "enter of the name of student", this appears:

Enter name of student: Enter Quiz 1:

I can't enter the student name anymore, which is NOT my desired result. :(

Any ideas? :(
Yes: the fgets also reads the trailing '\n' char, that you have to eliminate by hand. Otherwise it remains in the buffer and spoils your next attempt to read.

Simply write a function that processes a string and removes the trailing '\n'. Then everything should work just fine.
Why not create 2 string variables?
One for firstname, another for lastname?
@minomic thank you for the reply, although I really don't understand what you're talking about. :/ sorry I just started programming >_<
@momothegreat I tried it but it's still the same output. :/
Ok, this works: add

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

after

printf("The student's average is: %f \n\n", studentAverage);

and don't forget to

#include <limits>
Im just confused why he/she is using scanf when he has cin.

Last edited on
Ok what about using the getline function?
Something like
getline(scanf(" %s", studentName));
Oh I forgot to tell adriyel about the getline, even though I used it in the code I tried... It is for sure the best alternative...

Anyway you don't need the scanf inside the getline, just

getline(cin, studentName);

So, in the end, this is the code I tested:

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
#include <stdio.h>
#include <iostream>
#include <string>
#include <limits>


using namespace std;


int main()
{

     float gradeArray [5];
     string studentName;
     float studentAverage;
     int i=0;

     while (i <= 10)

     {
          printf("Enter the name of the student: ");
          getline(cin, studentName);

          printf("Enter quiz 1: ");
          scanf("%f", &gradeArray[0]);

          printf("Enter quiz 2: ");
          scanf("%f", &gradeArray[1]);

          printf("Enter quiz 3: ");
          scanf("%f", &gradeArray[2]);

          printf("Enter quiz 4: ");
          scanf("%f", &gradeArray[3]);

          printf("Enter quiz 5: ");
          scanf("%f", &gradeArray[4]);

          studentAverage= ((gradeArray[0]+ gradeArray[1]+ gradeArray[2] + gradeArray[3] + gradeArray[4]) / (5));
          printf("The student's average is: %f \n\n", studentAverage);

          std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

          i++;
     }

     return 0;

}


P.S. I am confused too about the mix made between C and C++. It's not incorrect, but I would stick with C++ and its cin/cout.
Topic archived. No new replies allowed.