Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cin

Posted on 1999-06-23
18
Medium Priority
?
443 Views
Last Modified: 2010-04-02
why does "cin" dosent work properly in the following program?

main()
{
  char ch[20];
  cout<<"Enter ch:";
  cin>>ch;
  cout<<ch;
  cout<<"again enter ch:";
  cin.get(ch,20);
  cout<<"the charecter entered is"<<ch;
  return();
}
0
Comment
Question by:rachana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +2
18 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1198306
What do you mean with properly?
0
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1198307
yeah, what do you expect it to ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1198308
I suspect your problems is that standard input (cin) is buffered.  This means that characters typed by the user are not "read" by the program until the user has pressed then enter key.  Thus the code

char ch;
cin >> ch;

does not continue after the user has typed a single character.  it waits until the user types 1 or more characters and presses enter.   This gives the user a chance to make corrections in what they type.

or perhaps the problem is that the code

char ch[20];
cin>>ch;

doesn't read an entire line.  It reads only 1 word and stops as soon as whitespace is encountered.  If you want to read an entire line, use the getline() function.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:Triskelion
ID: 1198309
I have tried multiple combinations of this and if you put the get first, the >> works.  The best suggestion is not to mix the two.  Also, return does not need () .

Maybe a better solution is not to use it.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1198310
What do you mean?  It works in either case.  What is happening that you think is wrong?
0
 

Accepted Solution

by:
rdf earned 20 total points
ID: 1198311
int main();
{
   char ch[20];
   cout<<"Enter character: "<<endl;
   for(int i = 0;i<20;i++)
     {
      cin>>char[i]<<endl;
      cout<<"You entered: "<<endl;
      cout<<char[i]<<emdl;
      }
return 0
}
0
 

Expert Comment

by:rdf
ID: 1198312
the **cout<<"Enter character: "<<endl;** should be the first line in the for loop just above ***cin>>char[i]<<endl;*** instead of above the for loop.
sorry
0
 
LVL 22

Expert Comment

by:nietod
ID: 1198313
rdf, if that fixes the problem, you must know what the problem was.  What was the problem?  
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1198314
Did you want string input or character input?
0
 

Expert Comment

by:rdf
ID: 1198315
I dont know what the problem is, i meant it as a comment, not as an answer, how do i change what i said to a comment????
thanks
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1198316
Hey rdf, have you tried compiling your answer?
Have you tried running it?
Please make your corrections, test and resubmit.

I think we may be losing focus here.
The original question (IMO) is:
   "Why does this specific example not work?"

0
 

Expert Comment

by:rdf
ID: 1198317
It compiled and ran on unix system, but it was meant as a comment because as nietod said I don't know what the problem is.
REJECT MY ANSER.
thanks
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1198318
Please forgive the addition of another message that is not the answer, but, I am talking about the syntax on the proposed answer.
1) The semi-colon after main()
2) The use of input operators >> and output << simultaneously.
3) "emdl" instead of "endl"
4) No final semi-colon after return 0

A better method would be to copy and paste directly from your IDE when submitting examples or answers.
0
 

Expert Comment

by:rdf
ID: 1198319
Sorry Triskelion, it was late and I wasn't very concise. I am a beginner, and I just wanted to enter a comment that maybe might work for the person asking the question. Below is the proper code for my comment, it is an exact copy from my unix file. I am trying to get my answer erased and changed to a comment, but I guess he or she figured it out for him or herself. If I have made any typos in what I have just said, or if my grammar is not exact, please excuse me, I'm just a dumb hick :):).


#define STOP_FOR(x) {char nob;cout<<"The value is  "<<x<<"  Any Key\
        to continue  ";cin>> nob;cout<<endl;}

   #include<iostream.h>
   #include<iomanip.h>
   #include<fstream.h>
   #include<stdio.h>
   #include<stdlib.h>
   #include<stddef.h>
   #include<string.h>
   #include<math.h>
   #include<ctype.h>
   #include<limits.h>


int main(){
       
        cout.setf(ios::fixed, ios::floatfield);
        cout.setf(ios::showpoint);
        cout << setprecision(2);

      char ch[20];
      for (int i = 0;i<20;i++)
         {
            cout<<"enter char: "<<endl;
            cin>>ch[i];
            cout<<"you entered: "<<endl;
            cout<<ch[i]<<endl;
         }      

        return 0;
}
                       




0
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1198320
what do you need this for:

#define STOP_FOR(x) {char nob;cout<<"The value is  "<<x<<"  Any Key\
        to continue  ";cin>> nob;cout<<endl;}

and these headers are totally irrelevant:
 
Comment
From: rdf Date: Sunday, June 27 1999 - 10:25PM CEST  
 
Sorry Triskelion, it was late and I wasn't very concise. I am a beginner, and I just wanted to enter a comment that maybe might work for the person asking the question. Below is the proper code for my comment, it is an exact copy from my unix file. I am trying to get my answer erased and changed to a comment, but I guess he or she figured it out for him or herself. If I have made any typos in what I have just said, or if my grammar is not exact, please excuse me, I'm just a dumb hick :):).


#define STOP_FOR(x) {char nob;cout<<"The value is  "<<x<<"  Any Key\
        to continue  ";cin>> nob;cout<<endl;}

and these headers are totally irrelevant:

   #include<iostream.h>
   #include<iomanip.h>
 
 
RACHANA:
please describe what you want your code to achieve, then we can help you.


0
 

Expert Comment

by:rdf
ID: 1198321
Look, I just made a little program to see if it would help Rachana out, ok? The #  deine is part of my program template that I use when I write programs. The #define stops the program and displays the value of the given variable in the parameter. It helps me see what and where a variable is and what it contains during different calls and soforth. It isn't hurting anything that I can tell. The #includes are there, they aren't hurting anything either, and I use them in case I re-use the program and add code which might need to acces these #include files. I didn't mean to confuse you Rachana, ignore them. They do not relate to your problem, but are handy to have. This is the way I feel, anyway.
I just wanted to help, didn't mean to upset everyone, I'm not an expert, I'm not even a bonafide c++ programmer, but I will try to help someone when I can. Everyone on this sight has helped me greatly as I progess, and they continue to do so. I meant no harm to anyone.
Rachana, REJECT MY ANSWER
rdf
0
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1198322
I did not mean to sound bitchy...I am probably still a beginner too. and the define macro does make sense. It was late for me too.
0
 

Expert Comment

by:rdf
ID: 1198323
:) cool, sorry also
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question