-2

I just started to learn C++. So there are many new things I do not know. My question is that how I can get 4 different numbers from the user and connect them with dots and print them. I have tried to research many different ways but I am not really sure what to use to start off so I am pretty confused. For example. My input numbers would be:

1

2

3

4

and the output should looked like:

1.2.3.4.

Thank you so much in advance!

Gimhani
  • 1,318
  • 13
  • 23
L.lil
  • 7

1 Answers1

2

Simply, to get input and print output to console, you can use cin and cout.

#include <stdio.h>
#include <iostream>
using namespace std; 

int main()
{
    int numbers[4];
    cout<<"Enter numbers"<<endl;
    for(int i=0; i<4; i++)
    {
      cin>>numbers[i];
    }
    cout<<"Your numbers are:\n";
    for(int i=0; i<4; i++)
    {
      cout<<numbers[i]<<".";
    }

    return 0;
}

Further, if you want to validate that actually numbers are entered, can update the 1st for loop as below.

    int numbers[4]={0,0,0,0};
    cout<<"Enter numbers"<<endl;
    for(int i=0; i<4; i++)
    {
      cin>>numbers[i];
      if(!cin.good())
      {
        cout<<"Invalid Numbers entered.\n";
        break;
      }

    }

Go through basic tutorials in c++ to learn about these (cin,cout, for loop, etc).

Gimhani
  • 1,318
  • 13
  • 23