Hi,

I would like to know if I could do this:

if i were to enter 5 numbers, it would go like this:
5
4
3
2
1

I want to know if I can do it like:
54321

Basically I don't want the insertion to go to a new line after pressing enter to input the first number. Is it possible?

Do you mean that you don't want to have to press enter after each (one character) number is entered? If so, then yes it is possible. I'm sure I have seen this done a different way, but here is one way anyhow:

char num1, num2, num3, num4, num5;
    cout << "enter 5 numbers:";
    num1 = cin.get();
    num2 = cin.get();
    num3 = cin.get();
    num4 = cin.get();
    num5 = cin.get();
 
    cout<<endl<<num1<<endl<<num2<<endl<<num3<<endl<<num4<<endl<<num5<<"\nPretty cool huh!\n";

You'll still have to press enter after the 5 numbers are entered, and this doesn't have error checking, of course. BUT if you enter 54321 (enter) then you will have your desired output. You could also enter a 5-letter word if you wanted to, since cin.get() gets one character.

Edited 6 Years Ago by MyrtleTurtle: n/a

Do you mean that you don't want to have to press enter after each (one character) number is entered?

I interpreted the OP's request differently. To the OP, you should clarify what exactly you are looking for. MyrtleTurtle might have interpreted in correctly and I didn't.

I thought the OP wanted the user to enter five numbers on one line and hit Enter five times. Each Enter separated the next number from the last number (i.e. Enter is a delimiter), yet the newline would not be echoed onto the screen.

It seemed a fairly odd request and I was puzzling over it. I was going to write up a response, but since that was just a GUESS of what the OP wanted, I'm going to hold off on that.

Also, please note whether you are using "number" and "digit" interchangeably. If each number is a single digit, that changes things considerably. If you want to just have the user enter five numbers on a single line, separated by spaces, then hit Enter once:

int num[5];
cout << "Enter five numbers on a single line : ";

for (int i = 0; i < 5; i++)
    cin >> num[i];

Do you mean that you don't want to have to press enter after each (one character) number is entered? If so, then yes it is possible. I'm sure I have seen this done a different way, but here is one way anyhow:

char num1, num2, num3, num4, num5;
    cout << "enter 5 numbers:";
    num1 = cin.get();
    num2 = cin.get();
    num3 = cin.get();
    num4 = cin.get();
    num5 = cin.get();
 
    cout<<endl<<num1<<endl<<num2<<endl<<num3<<endl<<num4<<endl<<num5<<"\nPretty cool huh!\n";

You'll still have to press enter after the 5 numbers are entered, and this doesn't have error checking, of course. BUT if you enter 54321 (enter) then you will have your desired output. You could also enter a 5-letter word if you wanted to, since cin.get() gets one character.

That doesn't enter 5 numbers. It enters 5 characters.

I'm more on Vernon's side. Simply enter the numbers as distinct numbers: Enter: 1 2 3 4 5

That doesn't enter 5 numbers. It enters 5 characters.

Well, shoot. You're right.

But, you can convert a character to a digit easily enough. And with Vernon's code you still have to press enter after each number is entered. Though I do like the idea of a for loop and an array rather than my repetitious code.

Edited 6 Years Ago by MyrtleTurtle: n/a

And with Vernon's code you still have to press enter after each number is entered.

One Enter is all that is needed. Enter 5 numbers separated by spaces, then press Enter once. See code below.

#include<iostream>
using namespace std;

int main()
{
    int num[5];
    cout << "Enter five numbers on a single line : ";

    for (int i = 0; i < 5; i++)
        cin >> num[i];

    cout << "\n\nHere is what you inputted\n\n";
    for (int i = 0; i < 5; i++)
    {
        cout << "num[" << i << "] = " << num[i] << endl;
    }

    return 0;
}
Comments
Thanks for the info. Some day I'll learn. :)

One Enter is all that is needed. Enter 5 numbers separated by spaces, then press Enter once. See code below.

As I said...

As I said...

Well, shoot. You're right.

(again) :icon_redface:

Edited 6 Years Ago by MyrtleTurtle: n/a

Comments
My comment was pointed at Vernon -- he said exactly what I said. You were agreeing. ;o)

One Enter is all that is needed. Enter 5 numbers separated by spaces, then press Enter once. See code below.

#include<iostream>
using namespace std;

int main()
{
    int num[5];
    cout << "Enter five numbers on a single line : ";

    for (int i = 0; i < 5; i++)
        cin >> num[i];

    cout << "\n\nHere is what you inputted\n\n";
    for (int i = 0; i < 5; i++)
    {
        cout << "num[" << i << "] = " << num[i] << endl;
    }

    return 0;
}

It pops up the error 'i redefinition'. I think you get what I mean, but i'll just explain to clarify:
I'm to enter 3 numbers, standard way would be:
5 (press enter)
1 (press enter)
2 (press enter)
and the display from entering the numbers would be:
5
1
2

but right now i want:
5 (press enter) 1(press enter) 2(press enter)
where the display would be
512

the main point of this is that i want my inputs all in one row. Thanks

:ooh: Oh, you just want to display it all on one line. Then you don't need to worry about how you get them, just how you display them.

When you cout<< the numbers, just don't print any spaces or endl (or "\n").

But...I don't think you can do this part all on one line, if that's what you mean:

5 (press enter) 1(press enter) 2(press enter)

That is what the 'enter' key does, after all...goes to a new line.

It pops up the error 'i redefinition'.

I would guess that is because of the second for loop, where you have 'int i' again. Just omit the int part. That code works fine for me as is, though.

Edited 6 Years Ago by MyrtleTurtle: n/a

It pops up the error 'i redefinition'.

It shouldn't. You should be able to copy and paste my program verbatim and compile it without error using a compiler like g++.

I'm to enter 3 numbers, standard way would be:
5 (press enter)
1 (press enter)
2 (press enter)

That is A standard way, not THE standard way. There's nothing that says that Enter needs to be entered between inputs unless you have a specific need for that. The program I posted, if it fits your needs, is the way to go, I think.

but right now i want:
5 (press enter) 1(press enter) 2(press enter)
where the display would be
512

Then the program I posted definitely would not work for your needs.

the main point of this is that i want my inputs all in one row. Thanks

If that's all you care about, then the program I posted WILL work.


Bottom line, I think, is that if you use cin, it's going to echo whatever is typed to the screen. If you want to require the Enter key to be pressed between inputs, but don't want it echoes onto the screen, I imagine that you'd need something like:

  1. conio.h
  2. ncurses
  3. Some type of redirection of stdin so it doesn't immediately echo to the screen.
  4. Some type of keyboard "hook".

It's all doable and there may be a way to do it without using the methods listed above, but I can't think of one right now.

But make sure you really need to do this. The program I wrote should compile and run as is and it'll put everything on one line.

Edited 6 Years Ago by VernonDozier: n/a

It shouldn't. You should be able to copy and paste my program verbatim and compile it without error using a compiler like g++.

That is A standard way, not THE standard way. There's nothing that says that Enter needs to be entered between inputs unless you have a specific need for that. The program I posted, if it fits your needs, is the way to go, I think.

Then the program I posted definitely would not work for your needs.

If that's all you care about, then the program I posted WILL work.


Bottom line, I think, is that if you use cin, it's going to echo whatever is typed to the screen. If you want to require the Enter key to be pressed between inputs, but don't want it echoes onto the screen, I imagine that you'd need something like:

  1. conio.h
  2. ncurses
  3. Some type of redirection of stdin so it doesn't immediately echo to the screen.
  4. Some type of keyboard "hook".

It's all doable and there may be a way to do it without using the methods listed above, but I can't think of one right now.

But make sure you really need to do this. The program I wrote should compile and run as is and it'll put everything on one line.

I'm using visual c++ 6.0 and i'm not really sure about the compiler thing.

Ok i can do it already. But here's what I want to know:
You can actually assign values to an array just by putting a space in between numbers/digits? what is the logic or understanding behind it?

Ok i can do it already. But here's what I want to know:
You can actually assign values to an array just by putting a space in between numbers/digits? what is the logic or understanding behind it?

How can you tell if 3756 is 3756 or 3, 7, 5, 6? If you can't, neither can scanf() . Therefore, use the format it understands.

This article has been dead for over six months. Start a new discussion instead.