Help program skipping

Feb 24, 2013 at 1:29am
MY program is skipping on the line that asks for the name is i put the full name with a space it will skip the rest and output everything without my input.
code :
#include <iostream.h>
#include <string>
#include <conio.h>
using namespace std;

int main(void)
{
int count = 0;
double total = 0.0;
double i = 0.0;
int infractions = -1;
// constant integer for number of classes
int numOfClasses = 0;
string name;
cout << "How many classes are you taking?:" << endl;
cin >> numOfClasses;

cout << "Please enter your grades (" << numOfClasses << "):" << endl;

while(count < numOfClasses) {
// stores input to temporary i double
cin >> i;
// this means total = total + i;
total += i;

cout << endl;
count++;
}

cout << "Enter your full name: ";
cin >> name;
cout << endl;

cout << "How many discipline infractions do you have?: ";
cin >> infractions;
cout << endl;

cout << "Your total points are " << total;
cout << endl;

double average = total/numOfClasses;
cout << "Your average..." << average << endl;
//
if(infractions == 0) {
if(average >= 90.0) {
if(numOfClasses >= 5) {
cout << " You are eligible because you have " << infractions << " infractions, " << average << " average, and are taking more than 5 classes. " << endl;
cout << " Congratulations! Good luck this year! " << endl;
} else {
cout << " You are ineligible because you do not have 5 or more classes." << endl;
cout << "I'm sorry just try again next time." << endl;
}
} else {
cout << "You are ineligible because you do not have a 90.0+ average." << endl;
cout << "Only if you did better." << endl;
}
} else {
cout << "You are ineligible." << endl;
cout << "I'm sorry, better luck next time." << endl;
}

getch();
return 0;
}
Feb 24, 2013 at 2:38am
The problem seems to be that you are using a string instead of a char array. Using a string, as soon as the user is entering the space bar to put in their last name, it is throwing the \0 terminator (I think) and causing it to run the rest of the program.... Anybody else care to share?
Feb 24, 2013 at 11:20am
@ justaguy: Absolutely not.

@ mayzuh: It's just a common RTFM case. You used cin >> name; which by definition reads only one word. You need to use getline() to read the entire line.
And learn to read what functions do before using them.
Topic archived. No new replies allowed.