944,083 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1494
  • C++ RSS
May 10th, 2007
0

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

Expand Post »
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NewKidWalking is offline Offline
7 posts
since Apr 2007
May 10th, 2007
0

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

remove the "\n"s -- that is only for cout. Not allowed to have literals (text in quotes) on the cin line.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
May 10th, 2007
0

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

Wow, that was right up there on the bone headed's list then. Thanx.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NewKidWalking is offline Offline
7 posts
since Apr 2007
May 10th, 2007
0

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

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?
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
May 10th, 2007
1

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

>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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 10th, 2007
0

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
~Ken Esquire~ is offline Offline
3 posts
since Oct 2006
May 10th, 2007
0

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

also dont use <iostream.h> , use <iostream>
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,528 posts
since Apr 2005
May 10th, 2007
0

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

C++ Syntax (Toggle Plain Text)
  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!!
Reputation Points: 29
Solved Threads: 2
Newbie Poster
iTaChi is offline Offline
21 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: frustrated newbie needs help
Next Thread in C++ Forum Timeline: code optimization ...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC