Help with C++ code please :)

I am trying to write a program that generates a table of temperature conversions. I am very new to C++ and I really need some help. I do know that there is error in my code and it will not compile. I haven't learned how to read the errors (and google hasn't helped) that command prompt alerts you to when compiling fails. So as you can imagine it's very hard to correct my errors, and I'm not even sure the code I have used will properly generate a table. Please help!

Errors displayed by command prompt:

other.cpp: In function" int main<>':
other.cpp:34:10:error: expected primary-expression before '=' token


Code:

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

int main()
{
double USD;
double PESO;
double RAND;
double YEN;

PESO=9.02; // rate = dollar per peso
RAND=0.01;
YEN=11.30;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);


cout << setw(10) << "Dollars" << setw(10) << "Pesos" << setw(10) << "rand" << setw(10) << "YEN" << setw(10) <<endl;

for(USD = 0.0; USD <= 50.0; USD += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}
for(PESO = 0.0; PESO <= 50.0; PESO += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
} for(RAND = 0.0; RAND <= 50.0; RAND += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
} for( = 0.0; YEN <= 50.0; YEN += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}
system("PAUSE");
return 0;
}
 
for( = 0.0; YEN <= 50.0; YEN += 1.0)


= 0.0 is the problem. You forgot to put a left value before the assignment operator.
Last edited on
Oh I should have started with 1 since I am trying to generate a table from 1 to 50 USD (and the second column the conversion in peso, third column rand, and forth yen) in increments of 1 USD. So here's my new code, but it still won't compile and gives me the same errors :

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

int main()
{
double USD;
double PESO;
double RAND;
double YEN;

PESO=9.02;
RAND=0.01;
YEN=11.30;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);


cout << setw(10) << "Dollars" << setw(10) << "Pesos" << setw(10) << "rand" << setw(10) << "YEN" << setw(10) <<endl;

for(USD = 1.0; USD <= 50.0; USD += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}
for(PESO = 1.0; PESO <= 50.0; PESO += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
} for(RAND = 1.0; RAND <= 50.0; RAND += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
} for( = 1.0; YEN <= 50.0; YEN += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}
system("PAUSE");
return 0;
Do you see how every other for loop is structured?

Like this:
for (initialization; condition; increase) { statements... };

The first part of that loop, initialization, is to define or initialize a variable. So instead of

1
2
3
4
for( = 1.0; YEN <= 50.0; YEN += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}


You would need to put a variable on the left side of the = 1.0, such as YEN or PESO or USD or even int i = 1.0, or you can leave it blank, either way, you cannot just have = 1.0;

Try:
1
2
3
4
for(YEN = 1.0; YEN <= 50.0; YEN += 1.0)
{
cout << setw(10) << USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}
Last edited on
Thank you soo much! It works! Just one last question I have the table set to end on row 50 however when I run the program it displays the 1-50 rows I want and a bunch of rows with 51 USD and the end, why is this happening? Thanks again!

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

int main()
{
double USD;
double PESO;
double RAND;
double YEN;

USD=1;
PESO=9.02;
RAND=0.01;
YEN=11.30;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);


cout << setw(10) << "Dollars" << setw(10) << "Pesos" << setw(10) << "rand" << setw(10) << "YEN" << setw(10) <<endl;


for(USD = 1.0; USD <= 50.0; USD += 1.0)
{
cout << setw(10) << USD << setw(10)<< USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}
for(PESO = 1.0; PESO <= 50.0; PESO += 1.0)
{
cout << setw(10) << USD << setw(10)<< USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}
for(RAND = 1.0; RAND <= 50.0; RAND += 1.0)
{
cout << setw(10) << USD << setw(10)<< USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}for(YEN = 1.0; YEN <= 50.0; YEN += 1.0)
{
cout << setw(10) << USD << setw(10)<< USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}
system("PAUSE");
return 0;
}
That's because you are re-running different for loops that are unnecessary.

Try to run just:
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
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include<cmath>
using namespace std;

int main()
{
double USD;
double PESO;
double RAND;
double YEN;

USD=1;
PESO=9.02;
RAND=0.01;
YEN=11.30;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);


cout << setw(10) << "Dollars" << setw(10) << "Pesos" << setw(10) << "rand" << setw(10) << "YEN" << setw(10) <<endl;

for(USD = 1.0; USD <= 50.0; USD++)
{
cout << setw(10) << USD << setw(10)<< USD*PESO << setw(10) << USD*RAND << setw(10) << USD* YEN << endl;
}

system("PAUSE");
return 0;
} 



Also in the future, wrap your code in code tags
Last edited on
You're using your curency variables (USD,PESO,RAND,YEN) as both loop indexes and a currency value. Using them as a loop index changes their value. When you drop out of the first for loop, USD has a value of 51, not a value of 1 as you had initially assigned.

If you had declared your currency variables as const values, your compiler would have detected your improper attempts to change them:
1
2
3
4
const double USD = 1.0;
const double PESO = 9.02;
const double RAND = 0.01;
const double YEN = 11.30;


Now for your loops, you need a separate index variable:
1
2
3
for(int i = 1; i <= 50; i++)
{   cout << setw(10) << i*USD << setw(10)<< i*PESO << setw(10) << i*RAND << setw(10) << i*YEN << endl;
}

Note sure why you have the other three loops, since the first one shows the value for 1-50 dollars in pesoes, rand and yen.

PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes it easier to refer to specific lines in your program.




Last edited on
Topic archived. No new replies allowed.