By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

No new line after cin

P: 1
I am attempting to fill in values for a matrix and I am using cin to input number types such as int or float one at a time. Unfortunately, when I try to input numbers into a large matrix, usually larger than 4 by 4, it is harder to visualize the matrix when after each input, a new line is generated . How can I prevent a new line from being generated after a cin so I can then use spaces or '\t' to indicate another cin.

Any help appreciated.
Feb 18 '07 #1
Share this Question
Share on Google+
2 Replies


AdrianH
Expert 100+
P: 1,251
I am attempting to fill in values for a matrix and I am using cin to input number types such as int or float one at a time. Unfortunately, when I try to input numbers into a large matrix, usually larger than 4 by 4, it is harder to visualize the matrix when after each input, a new line is generated . How can I prevent a new line from being generated after a cin so I can then use spaces or '\t' to indicate another cin.

Any help appreciated.
Hi JasonV,

cin will automaticly seperate by whitespace. That includes carrige returns, tabs regular spaces and vertical tabs (which are hardly ever used). So if you read in 4 numbers in you code and the input has seperated some of those 4 numbers with carrige returns, then it doesn't matter, all 4 will be read.

What you need to do is detect if you have reached the end of the matrix. You could just read in numbers till the stream closes. You could use peek() to determine if there is something other than a number. There are probably other ways to do this too. The way you do it is up to you.

Hope this helps.


Adrian
Feb 18 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
You can also input your information 4 numbers at a time. If your prompt looked something like this:

Expand|Select|Wrap|Line Numbers
  1. Please enter your 16 values:
you can enter the numbers as:

Expand|Select|Wrap|Line Numbers
  1. Please enter your 16 values:
  2. 1 2 3 4
  3. 5 6 7 8
  4. 9 10 11 12
  5. 13 14 15 16
This should help you to visualize what's going on, and cin won't have any problems with this.
Feb 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.