954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

#include <iostream.h>
char first[ 20 ];
char middle[ 20 ];
char last[ 20 ];
main()
{
    cout << "What is your name ScumBag? ";
    cin >> first >> "\n" >> middle >> "\n" >> last;
    return 0;
}
NewKidWalking
Newbie Poster
7 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

remove the "\n"s -- that is only for cout. Not allowed to have literals (text in quotes) on the cin line.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Wow, that was right up there on the bone headed's list then. Thanx.

NewKidWalking
Newbie Poster
7 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 
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?

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

>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:

class scan {
public:
  scan ( const char *init ): fmt ( init ) {}
  friend istream& operator>> ( istream& in, const scan& s )
  {
    while ( *s.fmt != '\0' && in && in.peek() == *s.fmt ) {
      in.get();
      ++s.fmt;
    }

    if ( *s.fmt != '\0' )
      in.setstate ( ios::failbit );

    return in;
  }
private:
  mutable const char *fmt;
};

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:

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
#include <iostream.h>
char first[ 20 ];
char middle[ 20 ];
char last[ 20 ];
main()
{
    cout << "What is your name ScumBag? ";
    cin >> first >> "\n" >> middle >> "\n" >> last;
    return 0;
}

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.

~Ken Esquire~
Newbie Poster
3 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

also dont use , use

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 
#include <iostream.h>
char first[ 20 ];
char middle[ 20 ];
char last[ 20 ];
main()
{
    cout << "What is your name ScumBag? ";
    cin >> first >> "\n" >> middle >> "\n" >> last;
    return 0;
}

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!!

iTaChi
Newbie Poster
21 posts since Mar 2007
Reputation Points: 29
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You