Display User's Input Horizontally

Is there a way for the user's input to be shown horizontally?
My desired output is

You may enter up to 50 integers:
How many would you like to enter? 12

Enter your number:
1 2 3 4 5 6 7 8 9 10 11 12

This is what I have done so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() {
	int n;
	cout << "You may enter up to 50 integers:" << endl;
	cout << "How many would you like to enter? " << flush;
	cin >> n;
	n -= 1;
	int array[n];

	cout << "Enter your number: " << endl;
	for (int i = 0; i <= n; i++) {
		cin >> array[i];
	}

		return 0;

	}

Last edited on
 
int array[n];


This isn't standard C++ - although allowed with some C++ compilers. For the code to be C++ standard, the number of elements in the array needs to be known by the compiler at compile time. If it's not known or there is no useful maximum, then consider using a std::vector instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main() {
	int array[50] {};
	int n {};

	cout << "You may enter up to 50 integers\n";
	cout << "How many would you like to enter: ";
	cin >> n;

	if (n <= 0 || n > 50) {
		cout << "Invalid number\n";
		return 1;
	}

	cout << "Enter your numbers: ";

	for (int i = 0; i < n; ++i)
		cin >> array[i];

	for (int i = 0; i < n; ++i)
		cout << array[i] << ' ';
}


Hello imyourjoy,

Since you did not post any code in your first message there was no need for this until now.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


This is not a fix to your program, but more an explanation of what you have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

int main()
{
    constexpr int MAXSIZE{ 50 };

    int numsToEnter{};

    std::cout << "\nYou may enter up to 50 integers:\n";
    std::cout << "\nHow many would you like to enter? ";
    std::cin >> numsToEnter;

    //numsToEnter -= 1;  // <--- Not really needed.

    int numbersArray[MAXSIZE]{};  // <--- Changed.

    std::cout << "\nEnter your number(s) separated by a space: ";  // <--- Changed.

    for (int idx = 0; idx < numsToEnter; idx++)  // <--- Changed.
    {
        std::cin >> numbersArray[idx];
    }

    std::cout << "\nYou Entered: ";

    for (int i = 0; i < numsToEnter; ++i)
        std::cout << numbersArray[i] << ' ';

    return 0;  // <--- Not required, but makes a good break point.
}

First it is best to include a whole program that can be compiled and tested. One of the easiest ways is to use the compiler available here. When you use code tags you will notice to the right of the box there is a gear icon with the words "Edit & Run". At least when it is working.

This will give people the opportunity to test your code without having to create a program of their own.

Next I want you to notice how a few blank lines break up the code and make it easier to read. Remember that you are not writing your code for the compiler, but for your-self and others to read, so make it as easy to read as you can.

As seeplus mentioned the standard C++ does not allow VLA, (Variable Length Arrays), but a few of the older compilers do.

When you define an array the compiler sets aside the proper amount of space on the stack, so this amount of space needs to be known at compile time. When yo write int numbersArray[MAXSIZE]{}; the number in the []s must be a constant number. Either a number like 10 or a variable defined as a constant.

The empty {}s will initialize the array so that it does not contain garbage values. While here it is always a good idea to initialize all your variables. Things like "std::string", "std::vector" and other containers are empty when defined and do not need initialized unless you want to give something a starting value.

Next is to give variables a good name. "n" may be quick and easy, but you are the only one that knows what it means. Others may have a different idea what "n" might be fore. Also when you programs become larger a good variable name will help you when writing your code. Then others when someone has to read it. Using the name "array" could be a problem as it could clash with something defined in a header file.

For the real problem.

You want your numbers all on 1 line, but the line cin >> array[i]; will not allow that. What happens is that you type in a number and press "Enter" to accept that entry. This will advance the cursor to the next line for the next input.

After testing seeplus code I tried this:

You may enter up to 50 integers:

How many would you like to enter? 5

Enter your number(s) separated by a space: 1 2 3 4 5

You Entered: 1 2 3 4 5


Then I realized.

Since the formatted input std::cin >> numbersArray[idx] will extract up to a white space or "\n" whichever comes first. Only the first number is put into the array and on the second loop any leading white space will be ignored putting the second number into the array and stopping at the next white space. You can use this to your advantage by telling the user how you want the user to enter the numbers before "Enter" is pressed.

Andy
Thank you guys for the help, appreciate it!
I have another example imyourjoy if this still interest you, only that mine is in
C
not
C++
, you can do it with storing numbers in a file and reproduce them to the next input in the same line, like you said
input to be shown horizontally
:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <conio.h>
#include <windows.h>

int main()
{
    FILE *file = NULL;
    char fname[256] = "test.txt";
    char numbers[50] = {0};
    int count = 0;

    printf("You may enter up to 50 integers\n");
    printf("Enter number of integers: ");
    scanf("%d", &count);

    if(count <= 0 || count > 50)
    {
        printf("\n\nInvalid number !\n");
        return 1;
    }

    printf("\nEnter your numbers: ");
    file = fopen(fname, "a+");

    for(int i = 0; i < count; ++i)
    {
        scanf("%s", numbers);
        fprintf(file, "%s ", numbers);

        for(int j = 0; j < count; ++j)
        {
            system("cls");
            printf("You may enter up to 50 integers\n");
            printf("Enter number of integers: %d\n", count);

            rewind(file);
            fgets(numbers, 49, file);
            printf("\nEnter your numbers: %s", numbers);
        }
    }

    fclose(file);
    remove("test.txt");

    printf("\n\nEnd !\n");
    printf("\nPress any key to close");
    _getch();

    return 0;
}


Sorry I work for now with C, but if I gather my minds a bit.. I think I could reproduce it in C++.
Last edited on
Topic archived. No new replies allowed.