Sign in to follow this  

Having a problem with C++

This topic is 3585 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


Link to 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


Link to post
Share on other sites
Quote:
Original post by rip-off
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.




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


Link to 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


Link to 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


Link to 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


Link to post
Share on other sites
Quote:
Original post by rip-off
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


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


Link to post
Share on other sites
Quote:
Original post by Brother Bob
Quote:
Original post by rip-off
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


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


Link to post
Share on other sites
Quote:
Original post by Brother Bob
Quote:
Original post by rip-off
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


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


My apologies for misinformation. However it is my personal style always to make these things as explicit as possible, to prevent subtle bugs from code that looks correct.

@OP: Googling your error code gives this. See if you can spot your error from that.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Original post by Brother Bob
Quote:
Original post by rip-off
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


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


My apologies for misinformation. However it is my personal style always to make these things as explicit as possible, to prevent subtle bugs from code that looks correct.

@OP: Googling your error code gives this. See if you can spot your error from that.


ok i found the error. so i added the backslashes. but do i still need to put the quotations around ;return 0; ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Skoorbnut
ok i found the error. so i added the backslashes. but do i still need to put the quotations around ;return 0; ?


I'm not sure what you mean. "return" is a keyword, it will cause the functions return value to be the value of the expression following it. The expression is terminated by a semicolon.

Example:

int main()
{
return 0;
}




For main(), you can omit the return, the C++ standard says that if you don't explicitly return a value from main(), it will return 0 for you. This only applies to main(), other functions must return a value if they have a non-void return type.

The following program is functionally equivalent to the above:

int main()
{
}


Share this post


Link to post
Share on other sites

This topic is 3585 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this