|
 |
|
 |
08-27-2003, 09:41 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Canada
Posts: 15
|
This should work so why doesn't it
I've been trying to get one of my program to work properly for a while now but it wont, to be honest I think the problem could be with my compiler (GCC 3.2.2) because this should work.
The problem doesn't occur until the first while loop runs throught once, then if I answer y to the prompt asking me if I want to add another distribution the program skips getline(cin,date);
Compiler information:
gcc version 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk)
Code:
#include <iostream>
#include <fstream>
#include <string>
int main() {
using namespace std;
// initialise variables
char responce = 'y'; // starts as yes so the loop will run at least once
string date;
string distribution;
string version;
while(toupper(responce) != 'N') {
system("clear"); // clear screen
cout << endl << "New list entry" << endl;
// get information
cout << "Date of release: ";
getline(cin,date);
cout << "Name of distribution: ";
getline(cin,distribution);
cout << "Version number: ";
getline(cin,version);
// write information
ofstream a_file("CS-FSLUG_dlist",ios::app);
a_file << date << ": " << distribution << " " << version << endl;
a_file.close();
// prompt for continuation
responce = 'p';
while (toupper(responce) != 'Y' && toupper(responce) != 'N') {
cout << "Enter another distribution (y/n): ";
cin >> responce;
}
system("clear"); // clear screen
}
return 0;
}
|
|
|
08-27-2003, 10:53 AM
|
#2 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
i would spend alot more time diagnosing a problem before you blame the compiler.
your problem is that this line:
cin >> responce;
only reads one char from the input, but the user entered 2 chars (example: 'y\n'). it is leaving the '\n' in the read buffer. the "getline(cin,date);" in the next loop is reading this '\n'. so, either absorb this newline char by another cin, or use a function more suited to reading the input you're expecting.
btw, it's spelled 'response'.
|
|
|
08-27-2003, 11:00 AM
|
#3 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
I think you should write a new program .. the 'Joe Bruin Spell Checker'
nice catch joe =) .. not to mention the spelling error in my code example yesterday. :rock:
__________________
Mike
|
|
|
08-27-2003, 11:19 AM
|
#4 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
there's nothing worse than working on someone else's code, and getting constant compile errors because you're spelling the variable names correctly that they originally misspelled. it's also a nice way to introduce errors. imagine having a global variable named 'response', a local var named 'responce', and accidentally typing it correctly. you can spend hours debugging that kind of crap.
|
|
|
08-27-2003, 11:26 AM
|
#5 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Canada
Posts: 15
|
Quote:
Originally posted by joe_bruin
i would spend alot more time diagnosing a problem before you blame the compiler.
your problem is that this line:
cin >> responce;
only reads one char from the input, but the user entered 2 chars (example: 'y\n'). it is leaving the '\n' in the read buffer. the "getline(cin,date);" in the next loop is reading this '\n'. so, either absorb this newline char by another cin, or use a function more suited to reading the input you're expecting.
btw, it's spelled 'response'.
|
I have no idea where you got that from, ther user does not at any point enter '\n' in fact there is no '\n' I used a forward slash because I knew that a backslash would be an escape charater. The user inputs either y or n but never '\n'.
As for the spelling mistake thats a minor problem.
BTW. I have had this problem before a number of times with GCC without a using 'cin << varname;' and that is why I'm think the problem might be the compiler itself
|
|
|
08-27-2003, 11:34 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Canada
Posts: 15
|
Quote:
Originally posted by joe_bruin
there's nothing worse than working on someone else's code, and getting constant compile errors because you're spelling the variable names correctly that they originally misspelled. it's also a nice way to introduce errors. imagine having a global variable named 'response', a local var named 'responce', and accidentally typing it correctly.
|
Where did you get that from, I did not have two variables with similar names.
Quote:
|
you can spend hours debugging that kind of crap.
|
I edited this message and removed the rest of this post because I missunderstood what you were saying and said things I shouldn't have
|
|
|
08-27-2003, 12:05 PM
|
#7 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
errr .. i would be happy that someone took to the time to look through my code and offer a suggestion ..
i don't think joe meant to bend you all out of shape as you seem to be.
he was using s hypathetical ( sp? ) example out of this situation. you just posted your code. you could have been the one who got the code with a mispelled global variable.
don't take it so personal.
__________________
Mike
|
|
|
08-27-2003, 12:09 PM
|
#8 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Canada
Posts: 15
|
Quote:
Originally posted by sde
errr .. i would be happy that someone took to the time to look through my code and offer a suggestion ..
i don't think joe meant to bend you all out of shape as you seem to be.
he was using s hypathetical ( sp? ) example out of this situation. you just posted your code. you could have been the one who got the code with a mispelled global variable.
don't take it so personal.
|
You'll have to excuse my temper, I had to put up with a lot in the Sun Microsystems java forums there were some real jerks there and I'm alway's a little on edge when dealing with web forums now.
|
|
|
08-27-2003, 12:17 PM
|
#9 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
Quote:
Originally posted by ntws01
there were some real jerks there
|
that is what we try to prevent here. we try to make an environment where people don't have to worry about being flammed and where the drama is kept to a minimum 
__________________
Mike
|
|
|
08-27-2003, 12:27 PM
|
#10 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Canada
Posts: 15
|
Quote:
Originally posted by sde
that is what we try to prevent here. we try to make an environment where people don't have to worry about being flammed and where the drama is kept to a minimum
|
O.k. I misunderstood his post, in a nutshell I'm really jumpy because people used to always run me down, at home, school and even on the internet. I stopped using IRC because I got made fun of for using Mandrake Linux, I stopped using usenet because it was full of trolls and spammers and I stopped using the Java forums because I was constantly being made fun of for not being very good at it (it was only my first year and I only had one book to learn from, what did they expect).
Sorry for the trouble, I honestly regret getting angry like that.
|
|
|
08-27-2003, 12:32 PM
|
#11 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
no trouble at all .. i wasn't referring to you .. just referring to how other forums out there are. they can make one very discouraged... but on the other hand.. there are some really great ones too .. and lots of people willing to help eachother out =) make me feel all warm and fuzzy inside 
__________________
Mike
|
|
|
08-27-2003, 12:36 PM
|
#12 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Canada
Posts: 15
|
Quote:
Originally posted by sde
no trouble at all .. i wasn't referring to you .. just referring to how other forums out there are. they can make one very discouraged... but on the other hand.. there are some really great ones too .. and lots of people willing to help eachother out =) make me feel all warm and fuzzy inside
|
Yeah but I feel bad about getting mad at joe_bruin, I missunderstood what he was trying to say and I reported him to abuse, is there any way to undo that?
|
|
|
08-27-2003, 12:43 PM
|
#13 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
Quote:
Originally posted by ntws01
You'll have to excuse my temper, I had to put up with a lot in the Sun Microsystems java forums there were some real jerks there and I'm alway's a little on edge when dealing with web forums now.
|
I hung out in those forums for a while. People don't go in there with a bad temper, they develop it, and with good reason.
|
|
|
08-27-2003, 12:44 PM
|
#14 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
I think sde IS abuse. So no worries.
Quote:
Originally posted by ntws01
Yeah but I feel bad about getting mad at joe_bruin, I missunderstood what he was trying to say and I reported him to abuse, is there any way to undo that?
|
|
|
|
08-27-2003, 12:47 PM
|
#15 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
I'm a 4+ CS major at a Java college, so I know a thing or two about it. If you need help, let me know (I'm all over the Java forum  ). But read the API first, Sun provides a GREAT documentaion base for Java, I can find almost everything I need there.
Quote:
Originally posted by ntws01
O.k. I misunderstood his post, in a nutshell I'm really jumpy because people used to always run me down, at home, school and even on the internet. I stopped using IRC because I got made fun of for using Mandrake Linux, I stopped using usenet because it was full of trolls and spammers and I stopped using the Java forums because I was constantly being made fun of for not being very good at it (it was only my first year and I only had one book to learn from, what did they expect).
Sorry for the trouble, I honestly regret getting angry like that.
|
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:56 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|