![]() |
Help with a small problem in C++
Hey guys,
In the past you guys have been extremely generous and helpful, not to mention very informative! And that's the reason why I keep on asking you guys for help! So I want to thank you guys in advance for your help! Ok onto the problem... I am creating a program for my C++ class that asks for a movie name and a bunch of ratings twice.. Then displays the average rating for each movie.. I got the code to work but with one small issue.. It only takes the first string from the cin.. So like if someone enters "American Pie" for example it will only read it as American... Now I managed to go around the issue and by using a small while loop and that worked for the first movie but it messed up the second cin... Here's the code so you can see what I'm talking about: Code:
#include <iostream> EDIT: I tried different things like using a different char (d instead of c) for the second movie.. I tried to even make another contructor called setMovieName2... It just does the same.. This is what's happening when I run the program: http://dl.farahfa.com/tmp/meh.jpg |
Re: Help with a small problem in C++
try the getline function:
http://en.cppreference.com/w/cpp/str...string/getline std::string moviename std::getline(std::cin, moviename); |
Re: Help with a small problem in C++
I should've mentioned that I've tried doing that and it didn't work either.. :/
I actually tried doing that before going for that small while loop... :/ |
Re: Help with a small problem in C++
Your main problem is that your cin.get(); on line 110 reads the '\n' that was left over in the input buffer after cin >> again; on line 103. You can try replacing the loop around the line 110 with something like
Code:
c = cin.get(); I am also a bit perplexed by the way you calculate the average rating, but I leave that bit to your tutor :) Lastly, your system("pause"); is not portable. But you probably already know that. |
Re: Help with a small problem in C++
@bandora, I took a look at your code, and i say.. make a function out of your loops
ex. loop() and run that twice(or evenmore) and incorporate some kind of database to get the data generated. |
Re: Help with a small problem in C++
Incidentally, for line-based input like yours, the best practice is to always read a line at a time using standard functions like getline() that consume the terminating '\n', then parsing the string, as opposed to parsing the standard input directly. That way you not only avoid surprises of the "'\n' left over in the buffer" kind, but also get extra robustness for cases when the user replies with "3, you silly old program" to queries requesting only a number (hint: cin << number will only swallow the 3, the rest including the comma will be left in the input buffer for the next cin operation to read).
|
Re: Help with a small problem in C++
Quote:
Here's the updated code: Code:
#include <iostream> http://dl.farahfa.com/tmp/meh2.jpg Also, would you care to elaborate what you meant about the average calculations please.. Are my calculations wrong? :/ Oh and also yes the system("pause"); is there only temporarily, only because when I start it without debugging the program would quit immediately after showing the averages! Also, @Humble, I really do appreciate your input, however I didn't want to go over the top by doing all that for this program because it's only a homework problem, so honestly I was just being lazy in regards to that! haha :D EDIT2: Creating another constructor named setMovieName2 worked!!! So now my question is.. Is the calculation for the average wrong?? I really would like to know because I honestly thought that there's nothing wrong with the calculations and now I am worried that I did it wrong lol.. |
Re: Help with a small problem in C++
Your movie names get concatenated. Why do you think this is? What do you use to contain the names and how do you initialize it? (Hinr: you use a single string and you get the name into it by appending one character at a time.) What happens when you enter a new name?
Regarding your average ratings, I find I do not understand how you calculate them. In your own example, you rated American Pie as 4 and Thor as 5. You gave each of them just one rating, so I would expect the averages to be 4 and 5, respectively. Why are they 0.2? Maybe it's as intended, but it's not what I would expect, which is why I said I'd leave it to your tutor. Humble is right, your code layout is a bit awkward. There is a lot of repetition. In general, if you find yourself writing the same code twice, you are doing something wrong. Even as few as two copies call for separating the common code into a function. Maybe I'm wrong. I do not know what your assignemt says exactly, but the way I would have approached is something like this (in pseudocode)... Code:
loop |
Re: Help with a small problem in C++
Quote:
Btw, I see what you mean about the average.. But if I am not asking for much could you give me a hint on how to do it? Like how do I get the program to count how many times I've entered a rating and divide by that number... Should I do a for loop in the main function when I enter the ratings? But if I do how do I return it to the getAverage() constructor..? That's not even possible right? Or do I change the void addRating(int num) constructor to a int addRating(int num) and return a value? Hmm.. I am a bit confused not to mention my brain is fried lol.. Too many homework due at the same time! D: :mad: |
Re: Help with a small problem in C++
Doing something even just twice is good enough a reason to make it a loop. 2 is a special case of N. If your assignment calls explicitly for doing it twice, unlikely as it sounds, then I would still write it as a loop that runs twice.
But enough of that, now to your averages. What is an average? A sum of all inputs divided by their number. Your assignment does not require to remember all the ratings, only the average. So you need just three variables. One temporary for reading the input, one for holding the sum of the inputs and one for holding their number. So get rid of MPAARating (it is not used anywhere) and replace numRate[5] with just two numbers, one for the sum and one for the number. Initialize both to 0, then each time the user enters a rating, add the rating to the sum and add 1 to the number. At the end, divide the sum with the number. |
All times are GMT. The time now is 19:16. |
vBulletin® Version 3.8.8