Total rookie here - need help with easy C++ stuff


#1

I enrolled in an intro to C++ course this semester and seems as though I’m completely lost. Have an assignment due in 3.5 hours and although it doesn’t look like I’ll be able to finish it in time, I’d still like to learn what I need to know for this assignment so I don’t get further behind.

Here’s what I’m looking to create:

"Write a program that accepts 2 integer values from the user, with the prompts shown.
For testing purposes, you have to output a newline after every user input."
Ex1
Enterthefirstinteger:10
Enterthesecondinteger:25
10+25=35
10*25=250
10/25=0
10%25=10

I know I’m supposed to utilize cout and cin but have no clue how to utilize them correctly. If anyonecan point me in the right direction it would be much appreciated


#2

start by typing cin examp and google will suggest cin example in c++ for which the first page has the following example

try running it in the ide

// i/o example

#include <iostream>
using namespace std;

int main ()
{
  int i;
  cout << "Please enter an integer value: ";
  cin >> i;
  cout << "The value you entered is " << i;
  cout << " and its double is " << i*2 << ".\n";
  return 0;
}

after playing with it for a bit you might come with something similar to the following code snippet:

#include <iostream>
using namespace std;

int main ()
{
  int a, b;
  cout << "Enter the first integer: ";
  cin >> a;
  cout << "\n";
  cout << "Enter the second integer: ";
  cin >> b;
  cout << a << " + " << b << " = " << a + b << ".\n";
  cout << a << " * " << b << " = " << a + b << ".\n";
  return 0;
}

#3

I think that’s exactly what I was going for, thank you so much! I’ll work on it now and see if I can get the hang of it

Update: That got me going on the right path, thank you again @harutyun !!


#4

@harutyun
If you could help me with this next exercise that would be fantastic, the code is compiling but seems like the variables arent being assigned as I was planning. Ill post the code with the result below.

(I’m not sure how I’m supposed to post code so it may come out differently than expected)

#include
using namespace std;

int main ()
{
int a, b, c, d, e, f;
cout << "Enter six fp numbers on a single line, separated by spaces: ";
cin >> a, b, c, d, e, f;
cout << “\n”;
cout << “Sum of " << a << " + " << b << " + " << c << " + " << d <<
” + " << e << " + " << f << " = " << a + b + c + d + e + f << “\n”;
cout << "Average " << " = " << ( a + b + c + d + e + f ) / 6 << “\n”;
return 0;
}

Result

Enter six fp numbers on a single line, separated by spaces: 1 2 3 4 5 6

Sum of 1 + 0 + 4196448 + 0 + 0 + 0 = 4196449
Average = 699408

EDIT

Not sure it’s the most efficient way to do it, but I separated each variable to its own line (cin >> a; cin >> b; etc.) and it works except when I enter non-integer numbers. Oh well, it’s close. I’m sure I’ll find the correct way soon enough