Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cin >> HELL!!!!!!!!!!!!!!!

Status
Not open for further replies.

pwel

Programmer
Aug 18, 1999
79
0
0
GB
Can anyone help with the following problem?<br>
<br>
The code works well up to line 3, after which the program jumps out and ends without letting me enter a <br>
number<br>
<br>
void main(void)<br>
{<br>
<br>
char cNameToAdd[15];<br>
char cNumToAdd[15];<br>
<br>
1. cout &lt;&lt; &quot;Please Enter A Name: \n&quot; ;<br>
2. cin.get(cNameToAdd, 15);<br>
3. cout &lt;&lt; cNameToAdd &lt;&lt; endl;<br>
4. cout &lt;&lt; &quot;Please Enter A Number: &quot; &lt;&lt; endl;<br>
5. cin.get(cNumToAdd, 15);<br>
6. cout &lt;&lt; cNumToAdd &lt;&lt; endl;<br>
7. cout &lt;&lt; cNumToAdd &lt;&lt; cNameToAdd &lt;&lt; endl;<br>
}<br>
<br>
Any help will be much appreciated.<br>
<br>
Paul welding.<br>
<br>
<br>
<br>

 
Hi,<br>
<br>
void main(void)<br>
{<br>
1 char cNameToAdd[15];<br>
2 char cNumToAdd[15];<br>
3 cout &lt;&lt; &quot;Please Enter A Name: \n&quot; ;<br>
4&gt;Use--&gt; cin.getline(cNameToAdd, 15);<br>
5 cout &lt;&lt; cNameToAdd &lt;&lt; endl;<br>
6 cout &lt;&lt; &quot;Please Enter A Number: &quot; &lt;&lt; endl;<br>
7 cin.get(cNumToAdd, 15);<br>
8 cout &lt;&lt; cNumToAdd &lt;&lt; endl;<br>
9 cout &lt;&lt; cNumToAdd &lt;&lt; cNameToAdd &lt;&lt; endl;<br>
}<br>
<br>
this should solve your problem.<br>
the reason being cin.get does not accept \n<br>
i.e. when u enter a name and press enter the name is assigned to cNameToAdd and &quot;\n&quot; or &quot;\r&quot; is assigned to<br>
cNumToAdd, so use cin.getline which has a default <br>
delimiter char as the newline character.<br>
All the best.<br>
<br>
<br>
-Sun<br>

 
Cheers -Sun.<br>
<br>
That sorted my problems out, i am a student, as you probably guessed!! and any help is much appreciated.<br>
<br>
Thanks again.<br>
<br>
Paul W.
 
its also a good idea to use cin.ignore(length,&quot;\n&quot;[or any other terminator) often to avoid getting a string of the last buffer attached to the new input<br>
for example the last buffer was 15 char, the new is 8, but strings attached to the remaining of the 15 char input.<br>
look into it, you'll see what i mean.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top