Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 08-27-2003, 09:41 AM   #1 (permalink)
ntws01
Registered User
 
ntws01's Avatar
 
Join Date: Mar 2003
Location: Canada
Posts: 15
ntws01 is on a distinguished road
Send a message via Yahoo to ntws01
Unhappy 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;
}
ntws01 is offline   Reply With Quote
Old 08-27-2003, 10:53 AM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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'.
joe_bruin is offline   Reply With Quote
Old 08-27-2003, 11:00 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-27-2003, 11:19 AM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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.
joe_bruin is offline   Reply With Quote
Old 08-27-2003, 11:26 AM   #5 (permalink)
ntws01
Registered User
 
ntws01's Avatar
 
Join Date: Mar 2003
Location: Canada
Posts: 15
ntws01 is on a distinguished road
Send a message via Yahoo to ntws01
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
ntws01 is offline   Reply With Quote
Old 08-27-2003, 11:34 AM   #6 (permalink)
ntws01
Registered User
 
ntws01's Avatar
 
Join Date: Mar 2003
Location: Canada
Posts: 15
ntws01 is on a distinguished road
Send a message via Yahoo to ntws01
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
ntws01 is offline   Reply With Quote
Old 08-27-2003, 12:05 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-27-2003, 12:09 PM   #8 (permalink)
ntws01
Registered User
 
ntws01's Avatar
 
Join Date: Mar 2003
Location: Canada
Posts: 15
ntws01 is on a distinguished road
Send a message via Yahoo to ntws01
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.
ntws01 is offline   Reply With Quote
Old 08-27-2003, 12:17 PM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-27-2003, 12:27 PM   #10 (permalink)
ntws01
Registered User
 
ntws01's Avatar
 
Join Date: Mar 2003
Location: Canada
Posts: 15
ntws01 is on a distinguished road
Send a message via Yahoo to ntws01
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.
ntws01 is offline   Reply With Quote
Old 08-27-2003, 12:32 PM   #11 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-27-2003, 12:36 PM   #12 (permalink)
ntws01
Registered User
 
ntws01's Avatar
 
Join Date: Mar 2003
Location: Canada
Posts: 15
ntws01 is on a distinguished road
Send a message via Yahoo to ntws01
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?
ntws01 is offline   Reply With Quote
Old 08-27-2003, 12:43 PM   #13 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-27-2003, 12:44 PM   #14 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
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?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-27-2003, 12:47 PM   #15 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do Multidimensional arrays work like a hash in Perl? philthee Java 1 10-22-2004 01:06 PM
ASP.NET: Day / Work Week / Week / Month web calendar control with view like MS Outloo gicio MS Technologies ( ASP, VB, C#, .NET ) 0 12-10-2003 10:05 AM
San Diego Tech. work. Admin Lounge 10 02-03-2003 06:19 PM
Getting X to work in Debian w00t Linux / BSD / OS X 7 08-25-2002 01:44 PM


All times are GMT -8. The time now is 11:56 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting