Hey, I got a very simple program here. Yet the compiler is disagreeing with me.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 7
Reputation: NewKidWalking is an unknown quantity at this point 
Solved Threads: 0
NewKidWalking NewKidWalking is offline Offline
Newbie Poster

Hey, I got a very simple program here. Yet the compiler is disagreeing with me.

 
0
  #1
May 10th, 2007
  1. #include <iostream.h>
  2. char first[ 20 ];
  3. char middle[ 20 ];
  4. char last[ 20 ];
  5. main()
  6. {
  7. cout << "What is your name ScumBag? ";
  8. cin >> first >> "\n" >> middle >> "\n" >> last;
  9. return 0;
  10. }
The aggrevation in codding, is that it does what I ask it to do, not what I want it to do.
The serenity in an idea, is though I was beaten to the punch, I was also assured to be on-track.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Hey, I got a very simple program here. Yet the compiler is disagreeing with me.

 
0
  #2
May 10th, 2007
remove the "\n"s -- that is only for cout. Not allowed to have literals (text in quotes) on the cin line.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 7
Reputation: NewKidWalking is an unknown quantity at this point 
Solved Threads: 0
NewKidWalking NewKidWalking is offline Offline
Newbie Poster

Re: Hey, I got a very simple program here. Yet the compiler is disagreeing with me.

 
0
  #3
May 10th, 2007
Wow, that was right up there on the bone headed's list then. Thanx.
The aggrevation in codding, is that it does what I ask it to do, not what I want it to do.
The serenity in an idea, is though I was beaten to the punch, I was also assured to be on-track.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Hey, I got a very simple program here. Yet the compiler is disagreeing with me.

 
0
  #4
May 10th, 2007
Originally Posted by Ancient Dragon View Post
remove the "\n"s -- that is only for cout. Not allowed to have literals (text in quotes) on the cin line.
There is some syntax of cin that allowes us to ignore the \ns.. Isn't it?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,730
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Hey, I got a very simple program here. Yet the compiler is disagreeing with me.

 
1
  #5
May 10th, 2007
>There is some syntax of cin that allowes us to ignore the \ns.. Isn't it?
You're probably thinking of the ws manipulator, but it discards all whitespace. If you want more control, you have to write your own manipulator:
  1. class scan {
  2. public:
  3. scan ( const char *init ): fmt ( init ) {}
  4. friend istream& operator>> ( istream& in, const scan& s )
  5. {
  6. while ( *s.fmt != '\0' && in && in.peek() == *s.fmt ) {
  7. in.get();
  8. ++s.fmt;
  9. }
  10.  
  11. if ( *s.fmt != '\0' )
  12. in.setstate ( ios::failbit );
  13.  
  14. return in;
  15. }
  16. private:
  17. mutable const char *fmt;
  18. };
And of course you can write custom manipulators so that they're used in exactly the same way as standard manipulators. This is only slightly more awkward than what the OP had before:
  1. cin>> first >> scan ( "\n" ) >> middle >> scan ( "\n" ) >> last;
Of course, that's effectively a no-op because most formatted input discards leading whitespace by default. That's why removing those strings altogether still works as expected.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: ~Ken Esquire~ is an unknown quantity at this point 
Solved Threads: 0
~Ken Esquire~ ~Ken Esquire~ is offline Offline
Newbie Poster

Re: Hey, I got a very simple program here. Yet the compiler is disagreeing with me.

 
0
  #6
May 10th, 2007
Originally Posted by NewKidWalking View Post
  1. #include <iostream.h>
  2. char first[ 20 ];
  3. char middle[ 20 ];
  4. char last[ 20 ];
  5. main()
  6. {
  7. cout << "What is your name ScumBag? ";
  8. cin >> first >> "\n" >> middle >> "\n" >> last;
  9. return 0;
  10. }
You should get rid of the newline in your input for starters also you may want to tell the user to put in their first, middle, last names because the way you have it the name will just fill up the allocated space you've given and go to the next without ever really designating a first, middle or last name just the size of the characters.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,219
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 538
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is online now Online
Moderator

Re: Hey, I got a very simple program here. Yet the compiler is disagreeing with me.

 
0
  #7
May 10th, 2007
also dont use <iostream.h> , use <iostream>
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 21
Reputation: iTaChi is an unknown quantity at this point 
Solved Threads: 2
iTaChi's Avatar
iTaChi iTaChi is offline Offline
Newbie Poster

Re: Hey, I got a very simple program here. Yet the compiler is disagreeing with me.

 
0
  #8
May 10th, 2007
Originally Posted by NewKidWalking View Post
  1. #include <iostream.h>
  2. char first[ 20 ];
  3. char middle[ 20 ];
  4. char last[ 20 ];
  5. main()
  6. {
  7. cout << "What is your name ScumBag? ";
  8. cin >> first >> "\n" >> middle >> "\n" >> last;
  9. return 0;
  10. }
hi...

the problem is, you cant put escape sequences in input functions like cin>>, it should be like this..

cin>>first;
cout<<"\n";
cin>>middle;
cout<<"\n";
cin>>last;

there..you should use cout<< for escape sequences...

good luck!!
"If you understand, you can learn anything"


http://www.cprogrammingdock.serverspeople.net
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC