#include <iostream>
using namespace std;
int main()
{
int i, integer;
cout << "Please input an integer ";
cin >> integer;
for (i = 1; i <= 10; i = i 1)
cout << "" << i << " x " << integer << " = " << i * integer;
cout << endl;
}
I am trying to make it display the input integer as eg.
1 x 4 = 4, 2 x 4 = 8
Instead I get
1 x 4 = 42 x 4 = 83
Not sure what I am doing wrong.
CodePudding user response:
There's no separation between one iteration and the next. So we print 1 x 4 = 4
immediately followed by 2 x 4 = 8
, giving 1 x 4 = 42 x 4 = 8
. We need to print some sort of separator; the most usual is to print each entry as a line, ending with a newline character:
#include <iostream>
int main()
{
int integer;
std::cout << "Please input an integer ";
std::cin >> integer;
if (!std::cin) {
// read error
return 1;
}
for (int i = 1; i <= 10; i) {
//