![]() |
|
#1
|
|||
|
|||
Grades Calculation ProgramHi guys,
I'm following an exercise out of a book: "Learn to Program with C++", author: John Smiley. The problem is this: I've followed the code out of the book exactly, but the program doesn't work. What it's supposed to do is after you answer "Yes" to the initial question, the body of the program under the while loop should execute. However, what happens is after you answer "yes", the program just quits, althought the while loop says otherwise. Here is the code (It's long - 280 lines! I think the problem may be within the first few lines of code, but I'm not sure): Thanks for the help, -'Mage. Code below: CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Grades Calculation Programhmm.. tried the suggestion that MichaelS-R gave...typing "YES" in all caps into the question field... the program ran, but quit (I saw a brief flash of another line of the program, and then it quit).
So, no joy. |
|
#3
|
||||
|
||||
Re: Grades Calculation Programtry adding system("pause"); before the return statement in the main function.
|
|
#4
|
|||
|
|||
Re: Grades Calculation ProgramI put "cin.get()" before every return statement in the program. Now what happens is when you say yes, it jumps to "Select student type" but then says immediately "you must select a student type".
-'Mage |
|
#5
|
||||
|
||||
Re: Grades Calculation ProgramThe problem is line 53:
CPP / C++ / C Code:
This works (I also changed a couple of ints to unsigned ints to keep the compiler happy.) CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: Grades Calculation ProgramQuote:
I'm not familiar with the book or the author, however this program is a really fine example of why it's not a Good Thing to mix cin>> and getline in a program. The program that you posted compiles and executes exactly as I think it is supposed to when I compiled it with the Borland bcc32 compiler. On the other hand, it exhibited precisely the behavior that you reported when I ran it with Microsoft visual C++ (both Visual Studio Version 6.0 and Visual C++ 2005 Express). Same bad behavior with GNU gcc. Stop! Don't think that Borland is always Good and Microsoft and GNU are always bad. Don't throw away whatever it is that you are using on the basis of behavior with this program. I hate to repeat myself, but I think this program is a pretty good example of what not to do about input data in C++. To me, the mystery is not so much: "Why didn't the other stuff work?" The mystery is: "Why did the Borland stuff work?" Another mystery might be: "Why would an author of a beginning textbook use a program that uses cin>> for reading a C++ std::string and then use the form of istream::getline( ) for reading into an array of char (treated as a C-style "string")?" How many confusing items could I put into a simple program? That's a rhetorical question. See footnote. So, I'll show a way that I think will give the results that you might want. I'm not claiming it's a better way of doing it, but it works with compilers for which I have access, and it uses consistent I/O. CPP / C++ / C Code:
I haven't changed anything significant other than the input routine for getting the "Ok to go" message from the user, and I won't comment on general style or anything else. I perceive from your question about the program ending with a brief flash that 1. You are running from some Integrated Development Environment (Maybe Microsoft, maybe dev-c++, maybe something else. Sometimes when you are asking for help, it helps us to help you if you tell us exactly what compiler you are using (and what operating system). You have no way of knowing whether the behavior is compiler-dependent or environment-dependent. Sometimes we can guess; sometimes --- not so much. 2. You haven't done much programming with this environment, since you (undoubtdedly) would have encountered the old "window closes when the program exits and I can't see what the heck it printed" behavior before. My suggestion is that you write a few simple programs (start with elementary book examples; then make up your own simple program. See what is required to keep the window open after the program exits. Write a few simple programs that input strings (C++ std::strings and/or C-style "strings"). Write a few simple programs that input numerical values (the world-famous Fahrenheit-do-Celsius conversion, for example, and I'll even give you bonus points for spelling "Fahrenheit " correctly.) I know it's disappointing to have a program in a book that doesn't work the way it is supposed to, and ones like this are particularly difficult to debug (I say this, based on the number of posts that I have seen with problems like this). Don't give up on a book just because there is one program that needs tweaking a little. I can tell you from experience that it's really easy to concoct a program that tests perfectly well on one system but fails on another system, sometimes due to things like mixing cin>> and getline. I have seen programs that apparently work OK on all systems, but I have seen others (like this one) that do not. By the same token, make sure you understand what's happening with book examples before going to the next. Sometimes it's easy to overlook important stuff in little programs in one's eagerness to get to the "good stuff". Regards, Dave Footnote: I sometimes get annoyed by authors who ask too many rhetorical questions; don't you? But wait! Wasn't that a rhetorical question? On the other hand, what if this is not a rhetorical question? |
|
#7
|
|||
|
|||
Re: Grades Calculation ProgramQuote:
The problem is mixing cin and getline and not taking everything into account. With your program and Microsoft or GNU compilers here's a program run: Code:
(It did pause after the "Thanks..." message, and that's good --- at least I can see that it didn't do what it was meant to do.) Rather than seeing what incremental changes might be fortunate to make it appear to work, I suggest that a programmer use either cin>> or getline(), but not both. I usually favor getline() where it makes a difference, but I use cin a lot also (just not in the same program as getline()). That's just my opinion of course, and Your Mileage May Vary. Regards, Dave P.S. When people try to give advice like this, and other people test it and find it doean't really seem necessary, it's easy to understand why the others ignore the advice. But some day, some way it will make a difference. Please: Don't take just my advice, or any other individual's. But you might want to keep this in mind when you go to a new computer system and a new compiler: Some of the stuff you learned to do is actually not OK. That's why I like this forum: I get to see things from lots of other people's point of view. I always learn something when I visit this place (always). P.S.S. Note that the modifications to the program that I showed in my post were designed to illustrate a way of working around the problem of mixing cin and getline() --- just say, "No!" There are lots of other things to do that could make the program more robust, regardless of which function is used for getting the user data, and I don't say that what I posted is a perfect example of a program of this type. But it did illustrate a point, I hope. D Last edited by davekw7x : 19-Jun-2006 at 14:19.
|
|
#8
|
|||
|
|||
Re: Grades Calculation ProgramDavekw7x:
I'm using the dev-C++ compiler ("Bloodshed") and I used it throughout my last-term C++ programming course, so I know that you have to put "system("PAUSE")" or "cin.get()" before your last return statement in the program to get everything to stay on-screen long enough for you to see. I tried your program, and it still gives me the error I previously described - jumps to the "You must select a student type" error message without the user entering any data into the program... I type "Yes" and it jumps to that. ... I've just been following all the other exercises in the book, and they've all worked without any problems. And what this exercise is is a modification of a previous exercise, the major mod being adding the while loop. Still completely stymied, -'Mage |
|
#9
|
||||
|
||||
Re: Grades Calculation ProgramDid you try my version?
|
|
#10
|
|||
|
|||
Re: Grades Calculation ProgramBlake:
Tried it, still got the same error. -'Mage |
Recent GIDBlog
Compress Your Web Site by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to read particular memory location ? | realnapster | C Programming Language | 10 | 10-May-2006 09:11 |
| Student ID/Grades program help... | kakamuti | C Programming Language | 13 | 13-Jan-2006 17:26 |
| creating a file in [c] | i hate c | C Programming Language | 15 | 21-Nov-2005 12:52 |
| Type casts ? | kai85 | C++ Forum | 12 | 23-Jun-2005 12:04 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 07:10 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The