Jump to content
• Advertisement

Public Group

# Having a problem with C++

This topic is 3868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hey i know you have heard this a million times but having a problem with one of my codes. I am using the book Sams Teach Yourself C++ in 21 days. Well i am following one of the tutorials and i cant figure out wht im doing wrong could you help. here is the program: // Listing 2.2 using std::cout #include <iostream> int main () { std::cout << "Hello there.\n"; std::cout << "Here is 5: " << 5 << "\n"; std::cout << "The manipulator std:: "; std::cout << "writes a new line to the screen. "; std::cout << std:: std::cout << "Here is a very big number:\t" <<70000; std::cout << std::cout << "Here is the sum of 8 and 5:\t"; std::cout << 8+5 << std:: ; std::cout << "Here is a fraction:\t\t"; std::cout << (float) 5/8 << std:: ; std::cout << "And a very big number:\t"; std::cout << (double) 7000 * 7000 <<std:: ; std::cout << "Dont forget to replace Jesse Liberty " ; std::cout << "with your name . . .\n"; std::cout << "Charles is a C++ programmer!\n; return 0; any ideas? im using Visual express C++ 2005

#### Share this post

##### Share on other sites
Advertisement
What's the problem?

#### Share this post

##### Share on other sites
Quote:
 Original post by SiCraneWhat's the problem?

Its not compiling and it says i have 9 errors. Sorry i should have put that.

#### Share this post

##### Share on other sites
First step to fixing the problem is describing it.

You haven't mentioned the problem you are having. Does it compile? If not, post the exact errors. Does it link? If not, post the errors. Does it crash at run time? If so, post the errors. Does it behave differently to what you expect? If so, tells us what did happen versus what you expected to happen.

Often, even doing this basic research (especially if you Google said errors) can help you figure out the problem on your own. Even if you can't - if you post the resulting information (error messages/whatever) then you have given us vastly more information with which to diagnose the error.

I can see some errors in your code, but I won't point them out until you have done this.

#### Share this post

##### Share on other sites
Quote:
 Original post by rip-offFirst step to fixing the problem is describing it.You haven't mentioned the problem you are having. Does it compile? If not, post the exact errors. Does it link? If not, post the errors. Does it crash at run time? If so, post the errors. Does it behave differently to what you expect? If so, tells us what did happen versus what you expected to happen.Often, even doing this basic research (especially if you Google said errors) can help you figure out the problem on your own. Even if you can't - if you post the resulting information (error messages/whatever) then you have given us vastly more information with which to diagnose the error.I can see some errors in your code, but I won't point them out until you have done this.

ok sorry :-/ well here is what it comes up with. Its linking fine. and i havent tryed to run it yet because its failed (sorry i have just started programming)

------ Build started: Project: chapter1, Configuration: Debug Win32 ------
Compiling...
using cout.cpp
c:\documents and settings\charles\my documents\visual studio 2005\projects\sams c++ 21 days\chapter1\using cout.cpp(10) : error C3083: 'std': the symbol to the left of a '::' must be a type
c:\documents and settings\charles\my documents\visual studio 2005\projects\sams c++ 21 days\chapter1\using cout.cpp(13) : error C2589: ';' : illegal token on right side of '::'
c:\documents and settings\charles\my documents\visual studio 2005\projects\sams c++ 21 days\chapter1\using cout.cpp(13) : error C2059: syntax error : '::'
c:\documents and settings\charles\my documents\visual studio 2005\projects\sams c++ 21 days\chapter1\using cout.cpp(15) : error C2589: ';' : illegal token on right side of '::'
c:\documents and settings\charles\my documents\visual studio 2005\projects\sams c++ 21 days\chapter1\using cout.cpp(15) : error C2059: syntax error : '::'
c:\documents and settings\charles\my documents\visual studio 2005\projects\sams c++ 21 days\chapter1\using cout.cpp(17) : error C2589: ';' : illegal token on right side of '::'
c:\documents and settings\charles\my documents\visual studio 2005\projects\sams c++ 21 days\chapter1\using cout.cpp(17) : error C2059: syntax error : '::'
c:\documents and settings\charles\my documents\visual studio 2005\projects\sams c++ 21 days\chapter1\using cout.cpp(20) : error C2001: newline in constant
c:\documents and settings\charles\my documents\visual studio 2005\projects\sams c++ 21 days\chapter1\using cout.cpp(21) : error C2143: syntax error : missing ';' before 'return'
Build log was saved at "file://c:\Documents and Settings\Charles\My Documents\Visual Studio 2005\Projects\Sams C++ 21 days\chapter1\chapter1\Debug\BuildLog.htm"
chapter1 - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

#### Share this post

##### Share on other sites
Ok. You have somehow missed the "endl" (END - L, meaning end line, often mistaken for end - 1 in some fonts) from your code. Also, you are missing the end of string delimiter " from one of your strings (marked "Here" in the comments).
#include <iostream>int main (){    std::cout << "Hello there.\n";    std::cout << "Here is 5: " << 5 << "\n";    std::cout << "The manipulator std::endl" << std::endl;    std::cout << "writes a new line to the screen.";    std::cout << std::endl;    std::cout << "Here is a very big number:\t" << 70000;    std::cout << std::endl;    std::cout << "Here is the sum of 8 and 5:\t";    std::cout << 8+5 << std::endl;    std::cout << "Here is a fraction:\t\t";    std::cout << (float) 5/8 << std::endl;    std::cout << "And a very big number:\t";    std::cout << (double) 7000 * 7000 <<std::endl;    std::cout << "Dont forget to replace Jesse Liberty " ;    std::cout << "with your name . . .\n";    std::cout << "Charles is a C++ programmer!\n"; // <-- Here!     return 0;}

Note: (float) 5/8 won't work for you. Divison of two integers in C++ yields integer division, it will discard any decimal part in the result. To do regular division, one (or both) of the operands must be a float (or double):
5.0f / 8.0f

Quote:
 ok sorry :-/

No need to apologise [smile]

It is general advice that will help you in the future when you have more problems. By describing the problem in full detail, you give everyone here more tools to help you. You'll get results faster too.

#### Share this post

##### Share on other sites
You appear to be missing a couple of things in your code. I've added in some comments. Most of the problems appear to be std:: perhaps you meant std::endl for a new line?
You're also missing a } at the end of your code.
// Listing 2.2 using std::cout#include <iostream>int main (){	std::cout << "Hello there.\n";	std::cout << "Here is 5: " << 5 << "\n";	std::cout << "The manipulator std:: ";            // Here	std::cout << "writes a new line to the screen. ";	std::cout << std::                                // Here	std::cout << "Here is a very big number:\t" <<70000;	std::cout <<                                      // Here	std::cout << "Here is the sum of 8 and 5:\t";	std::cout << 8+5 << std:: ;                 // Here	std::cout << "Here is a fraction:\t\t";	std::cout << (float) 5/8 << std:: ;         // Here	std::cout << "And a very big number:\t";	std::cout << (double) 7000 * 7000 <<std:: ; // Here	std::cout << "Dont forget to replace Jesse Liberty " ;	std::cout << "with your name . . .\n";	std::cout << "Charles is a C++ programmer!\n;		return 0;}

[Edit:] Beaten

#### Share this post

##### Share on other sites
Quote:
Original post by rip-off
Ok. You have somehow missed the "endl" (END - L, meaning end line, often mistake for end - 1 in some fonts) from your code. Also, you are missing the end of string delimiter " from one of your strings (marked "Here" in the comments).
*** Source Snippet Removed ***

Note: (float) 5/8 won't work for you. Divison of two integers in C++ yields integer division, it will discard any decimal part in the result. To do regular division, one (or both) of the operands must be a float (or double):
5.0f / 8.0f

Quote:
 ok sorry :-/

No need to apologise [smile]

It is general advice that will help you in the future when you have more problems. By describing the problem in full detail, you give everyone here more tools to help you. You'll get results faster too.

awesome! thanks i will put that in now

#### Share this post

##### Share on other sites
Quote:
 Original post by rip-offNote: (float) 5/8 won't work for you. Divison of two integers in C++ yields integer division, it will discard any decimal part in the result. To do regular division, one (or both) of the operands must be a float (or double):5.0f / 8.0f

Casting got higher precedence than division, so that code was perfectly OK. 5 is cast to a float and then divided by an integer.

#### Share this post

##### Share on other sites
Quote:
Original post by Brother Bob
Quote:
 Original post by rip-offNote: (float) 5/8 won't work for you. Divison of two integers in C++ yields integer division, it will discard any decimal part in the result. To do regular division, one (or both) of the operands must be a float (or double):5.0f / 8.0f

Casting got higher precedence than division, so that code was perfectly OK. 5 is cast to a float and then divided by an integer.

alrigt sorry a have another question. i have narrowed it down to one error. not sure what it means

error C2001: newline in constant

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5
• Advertisement

• 33
• 13
• 10
• 10
• 12
• ### Forum Statistics

• Total Topics
632569
• Total Posts
3007121

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!