counter-controlled while loop c++

hey guys help me please :)

The program should be print the numbers 0 through 10, along with their values doubled and tripled. The data file contains the necessary variable declarations and output statements.

Example the output: Single Double Triple 1 2 3 2 4 6 3 6 9 4 8 12


here my code tell me if correct

#include <iostream>
#include <cstdlib>

using namespace std;

int main ()

{


int x, double, triple;
int x = 0
while (x<=10)

{
cout << "x" << double = (x*2) << triple = (x*3) << endl;
x++;
}
return EXIT_SUCCESS
Last edited on
Please use proper code tags around your code.
First of all you can not name a variable double since C++ recognizes it as a type,
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
#include <iostream>
#include <cstdlib>
#include <iomanip> // for the function setw(x); which sets x spaces

using namespace std;

int main (){

int x = 0, double1, triple;  //Rename double to anything else

// int x = 0 ;  Forgot semicolon here and you can not declare the same variable twice. What you can do is x = 0 with out int, or do what I did the line above

while (x<=10){
//Here you want to cout x the variable and not "x" the character.
//You can not initialize and cout at the same time, so initialize the variables //then cout them.

double1 = (x*2);
triple = (x*3);
cout << x << setw(5) << double1 <<setw(5) << triple  << endl;
x++;
}

return 0; // Missing semicolon here and just return 0 instead of EXIT_SUCCESS, its more common

} // you missed ending bracket

You should put your code between [ code ][ /code ] tags to make it readable, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
	int x, double, triple; // double is a keyword
	int x = 0 // ; needed
	while (x<=10)
	{
                /*
                 * You need to put "" in here, look at my code.
                 * Unless you're trying to assign double and triple
                 * and print them at the same time?
                 */
		cout << "x" << double = (x*2) << triple = (x*3) << endl;
		x++;
	}
	return EXIT_SUCCESS // ; needed
}


Your code has quite a few errors... I'll fix it for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
	int x = 0; //double is a keyword, and isn't needed
	while (x<=10)
	{
                /*
                 * You need to separate text and variables,
                 * also, you don't need "double" or "triple", so I've removed them
                 */
		cout << "single = " << x << " double= " << (x*2) << " triple= " << (x*3) << endl;
		x++;
	}
	return EXIT_SUCCESS;
}
Last edited on
@valliusDax sir thanks.. help me please :)
Last edited on
Jasis Sanqui wrote:
@valliusDax sir thanks.. help me please :)

I'm not sure what more help you need.

I've done the code one more time and matched it to exactly what you wanted from your first post.

This will output exactly:
Single Double Triple
     1     2     3
     2     4     6
     3     6     9
     4     8    12
     5    10    15
     6    12    18
     7    14    21
     8    16    24
     9    18    27
    10    20    30
    11    22    33


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
	int x = 0;
	cout << "Single Double Triple" << endl;
	while (x++<=10)
	{
		cout << setw(6) << x 
		     << setw(6) <<(x*2) 
		     << setw(6) <<(x*3)
		     << endl;
	}
	return EXIT_SUCCESS;
}
Last edited on
ok thanks sir :) i have a idea now..
Topic archived. No new replies allowed.