End of chapter problem

I tried avoiding posting here again, since this is my fourth topic today.
Spent 15 minutess gazing at the code, fixing typos.
Now I cannot find any and it still wont work.

Code=
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std ;

int main()
{
const double PI = 3.1415926536
cout << "6\" circle circumference: " << (PI * 6) << endl;
enum
{ RED=1, YELLOW, GREEN, BROWN, BLUE, PINK, BLACK } ;
cout << "I shot a red worth: " << RED << endl ;
cout << "Then a blue worth: " << BLUE << endl ;
cout << Total scored: " << ( RED + BLUE ) << endl ;
typedef enum { NEGATIVE , POSITIVE } charge ;
charge neutral = NEGATIVE , live = POSITIVE ;
cout << "Neutral wire: " << neutral << endl ;
cout << "Live wire: " << live << endl ;
 return 0 ;
} 


And cmd said..
1
2
3
4
5
6
7
8
9
10
11
C:\My Programs>c++ constant.cpp -o constant.exe
constant.cpp: In function `int main()':
constant.cpp:7: error: expected `,' or `;' before "cout"
constant.cpp:12: error: `Total' was not declared in this scope
constant.cpp:12: error: expected `;' before "scored"
constant.cpp:12: error: missing terminating " character
constant.cpp:13: error: `charge' was not declared in this scope
constant.cpp:14: error: expected `;' before "neutral"
constant.cpp:15: error: `neutral' was not declared in this scope
constant.cpp:16: error: `live' was not declared in this scope
constant.cpp:18:2: warning: no newline at end of file 


Im at the end of the first chapter though, and probably will stop trying today after this is fixed. Then ill go over everything ive done. =3.
Thank you.
Watch the syntax highlighting. There's an opening quote missing on line 12.

Why don't you keep posting on the same thread instead of making thread after thread? They're all more or less the same, anyway.
Anyways.. Ill post all of the problems later I get, after this one is fixed xD.
The code now is [added quote]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std ;

int main()
{
const double PI = 3.1415926536
cout << "6\" circle circumference: " << (PI * 6) <<  endl ;
enum
{ RED=1, YELLOW, GREEN, BROWN, BLUE, PINK, BLACK } ;
cout << "I shot a red worth: " << RED << endl ;
cout << "Then a blue worth: " << BLUE << endl ;
cout << "Total scored: " << ( RED + BLUE ) << endl ;
typedef enum { NEGATIVE , POSITIVE } charge ;
charge neutral = NEGATIVE , live = POSITIVE ;
cout << "Neutral wire: " << neutral << endl ;
cout << "Live wire: " << live << endl ;
 return 0 ;
}


And cmd says:
1
2
3
4
C:\My Programs>c++ constant.cpp -o constant.exe
constant.cpp: In function `int main()':
constant.cpp:8: error: expected `,' or `;' before "co
constant.cpp:19:2: warning: no newline at end of file 
Last edited on
constant.cpp:7 - missing ';' after line 6
constant.cpp:12 - missing " for the cout

constant.cpp:13: error: `charge' was not declared in this scope
constant.cpp:14: error: expected `;' before "neutral"
constant.cpp:15: error: `neutral' was not declared in this scope
constant.cpp:16: error: `live' was not declared in this scope

^^ im pretty sure ur syntax is way messed up for line 14.

charge neutral = NEGATIVE , live = POSITIVE ;




ah. guess i was wrong. disregard. i took a crack.
I get this straight from a C++ book, and sometimes its wrong.
Um..

Does anyone know how to fix it?
Ah, here we go. There's a semicolon missing at the end of line 6.

Next time be more careful while copying and make sure you post the entire error message. before "co and before "cout" suggest different things.
Im posting this straight from the book, and it is exactly what the book says.
I cannot find the error :[

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std ;

int main()
{
num = 1 + 4 * 3;
cout << endl << "Default order: " << num << endl ;
num = ( 1 + 4 ) * 3 ;
cout << "Forced order: " << num << endl << endl ;
num = 7 - 4 + 2 ;
cout << "Default direction: " << num << endl ;
num = 7 - ( 4 + 2 ) ;
cout << "Forced direction: " << num << endl ;
 return 0 ;
}


Cmd's responce is:
1
2
3
4
C:\My Programs>c++ precendence.cpp -o precendence.exe
precendence.cpp: In function `int main()':
precendence.cpp:6: error: `num' was not declared in this scope
precendence.cpp:15:2: warning: no newline at end of file


I cannot find what is wrong with the "num" on line 6.
Last edited on
You aren't declaring the variable 'num' anywhere.

Assuming 'num' is an integer, line 6 should read:

 
int num = 1 + 4 * 3;


Yeah..
Thanks for finding the type/wrong thing in this book.
(It seriously says "num = 1 + 4 * 3;") :/
Another problem ._.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std ;


int main()
{
int num = 3 ;
switch ( num )
{
case 1 : cout << num << " : Monday" ; break ;
case 2 : cout << num << " : Tuesday" ; break ;
case 3 : cout << num << " : Wednesday" ; break ;
case 4 : cout << num << " : Thursday" ; break ;
case 5 : cout << num << " : Friday" ; break ;
}
 return 0 ;
}


It then says "In the switch statement, insert a default statement after the final case statement." Which just confused me.. I ended up with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std ;


int main()
{
int num = 3 ;
switch ( num )
{
case 1 : cout << num << " : Monday" ; break ;
case 2 : cout << num << " : Tuesday" ; break ;
case 3 : cout << num << " : Wednesday" ; break ;
case 4 : cout << num << " : Thursday" ; break ;
case 5 : cout << num << " : Friday" ; break ;
default : cout << num " : Weekend day" ;
}
 return 0 ;
}


And CMD said:
1
2
3
C:\My Programs>c++ switch.cpp -o switch.exe
switch.cpp: In function `int main()':
switch.cpp:15: error: expected `;' before string constant


How can I fix this?
You forgot the << after num.

It's not that I'm getting tired of this or anything, but I think you're becoming dependent on us. Try to read the error messages more carefully. They are very descriptive, most of the time.
I am becoming dependant on this forum whenever I get stuck using this book. Without you guys I would still be stuck on page 2 :o

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std ;
int main ()

float fToC ( float degreesF )
{
float degreesC = ( (5.0 / 9.0 ) * ( degreesF - 32.0 ) ) 

;
 return degreesC ;
float fToC ( float degreesF= 32.0 ) ;
{
float fahrenheit, centigrade ;
cout << "Enter a Fahrenheit temperature:\t" ;
cin >> fahrenheit
centigrade = fToC( fahrenheit ) ; 
}
cout << fahrenheit << "F is " << centigrade << "C" ;
cout << endl << "Freezing point: " << fToC() << "C" ;
 return 0 ;
}


CMD says

1
2
C:\My Programs>C++ args.cpp -o args.exe
args.cpp:5: error: expected initializer before "float"


Im basically rushing through this book, and I can't figure out what a initializer is. I kept thinking it was the "{", but I was wrong. ._.
Last edited on
You've tried to create a function (fToC) inside main(). I was going to say move it above main() and it should be fine but reading further on you've got a bit confused somewhere. I think you need to look over that code again and try and understand what you have written before I give some help. I don't mean to sound rude but you'll learn more - you must have just been unfocused at the time.

Line 5 and 11 in particular... but there are a few other errors. I recommend re-writing this program honestly as it is only small and you can have a fresh start.

Read this: http://www.cplusplus.com/doc/tutorial/variables/
Scroll down to Initialization of variables
Last edited on
Topic archived. No new replies allowed.