int main()
{
vector<string> names;
vector<double> grades;
string name;
cout << "Enter your names: ";
while (cin >> name) {
names.push_back(name);
if (name == "q") break;
}
int i = 0;
while (i < names.size())
{
cout << names + ", enter you grade: ";
cin >> grades;
cout << endl;
i++;
}
system("PAUSE");
return 0;
}
this doesn't work, for some reason. It works perfectly up until I enter the grade for the first student, and after I press enter, there's a Windows error and the program just shuts down. I think it's obvious what I'm trying to do here, so why doesn't it work? I can't think of a single reason, apart from maybe some type mismatch error... Please help! I tried using a for loop, but the result is the same.
[C++] What's wrong with code?
Hey everyone! Could someone please help me out with this...
grades is empty; trying to access it with grades when i == 0 will throw an exception.
Try changing:
cin >> grades;
to:
double tmp;
cin >> tmp;
grades.push_back( tmp );
EDIT:
If you're using Dev-C++, I'd advise you to get rid of it. Use Visual Studio 2008 Express Edition instead.
Try changing:
cin >> grades;
to:
double tmp;
cin >> tmp;
grades.push_back( tmp );
EDIT:
If you're using Dev-C++, I'd advise you to get rid of it. Use Visual Studio 2008 Express Edition instead.
As _fastcall said ... grades has 0 elements so you have to use push_back() every time you read a grade. You can use an auxiliary variable like this :
double aux;
cin>>aux;
grades.push_back(aux);
double aux;
cin>>aux;
grades.push_back(aux);
I'm pretty sure there's a way to resize the vector, I'm probably wrong though.
It'd mean you wouldn't need the temporary variable and you can just continue as you were.
It'd mean you wouldn't need the temporary variable and you can just continue as you were.
Quote:Original post by MrPickle
I'm pretty sure there's a way to resize the vector, I'm probably wrong though.
Like the resize() member function?
Quote:Original post by MrPickle
I'm pretty sure there's a way to resize the vector, I'm probably wrong though.
Nope, you are quite correct:
int i = 0; grades.resize( names.size() );while (i < names.size()){ cin >> grades; // Dot dot dot}
Quote:Original post by MrPickle
I'm pretty sure there's a way to resize the vector
There is.
Edit: Much too slow, I must need more coffee.
Wow, thank you guys! You're very very helpful here! And there's so much I have already learned just by doing this seemingly pointless Accelerated C++ exercise!
I found three things that aren't necessarily wrong, but would in the end help you.
1. You aren't checking the value of the name before pushing it.
You should check to see if name==q first if it isn't then you push the name on to the vector.
2. If you think about this as a real world application with hundreds of different names/grades you may want to just be able to input both values at the same time. e.g. get a name then get a value for that name. (You could use a map for this but two vectors will work just as well).
3. You may want to check if q is upper case or lower case.
(For future reference there is a string function that can compare 2 strings for you. http://www.cplusplus.com/reference/string/string/compare/)
Also for your last while loop it would be the same as using a for loop.
1. You aren't checking the value of the name before pushing it.
names.push_back(name);if (name == "q") break;
You should check to see if name==q first if it isn't then you push the name on to the vector.
2. If you think about this as a real world application with hundreds of different names/grades you may want to just be able to input both values at the same time. e.g. get a name then get a value for that name. (You could use a map for this but two vectors will work just as well).
3. You may want to check if q is upper case or lower case.
if (name == "q"||name=="Q") break;
(For future reference there is a string function that can compare 2 strings for you. http://www.cplusplus.com/reference/string/string/compare/)
Also for your last while loop it would be the same as using a for loop.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement