Ask your own question, for FREE!
Computer Science 21 Online
OpenStudy (psymon):

New to C++, so even the easy stuff I'm clueless about. Help?

OpenStudy (psymon):

So, i basically have no idea what I'm required to have in the program to even get it to compile properly. I tried putting in what it looks like is needed based on this handout, but I keep getting errors when I try to compile.

OpenStudy (psymon):

#include <iostream> using namespace std; int main () { return 0; } I would think this would be the starting point, but this info isnt formatted rightin some way, it just wont compile and I dont know what else is needed.

OpenStudy (e.mccormick):

``` #include <iostream> using namespace std; int main() { cout << "Hello World!"; cin.sync(); cin.ignore(); return 0; } ``` The sync/ignore makes it wait for you to press enter before exiting. If running in a windowed environment this is useful because the program can run and close before you see it.

OpenStudy (psymon):

I've been sloooowly getting it to do what I want, but still trying to get a few things to work. So I'm supposed to make it as to where the program prompts me to input some values and then the program calculates based on the formulas I put in and then it gives me the correct output. As of now, Im trying to figure out how to get it to display the numbers with an actual % sign after them. So like if I want the output to show up as 4% or something, Im trying to figure out how to have it do that. Every time I try I cant get it to show up. Is it because itll never output a percent sign if I use double values or something?

OpenStudy (e.mccormick):

What are you doing to get it to output a %?

OpenStudy (psymon):

Well, the program, at least how ive tried to do it, wont let me put a percent sign when it asks for my input. If I do, it skips the next prompt for input and just spits out all 0's for the values I want calculated. If I try to get it to tack on the % sign during the output, it doesnt show or shows up in the wrong spot. I figured out how to get the sign to be in front of the numbers, like %4, but I need it to be 4%. Im still tinkering with it, so not sure if Id figure it out on my own or not.

OpenStudy (e.mccormick):

On input the problems is that % is not a number, it is a character. On output: ``` #include <iostream> using namespace std; int main() { int a = 5; double b = 72.6; cout << a << "%\n" << b << "%\n\n"; cin.sync(); cin.get(); return 0; } ``` The \n is to make a new line.

OpenStudy (e.mccormick):

There are a number of escaped characters like that: http://www.cplusplus.com/doc/tutorial/constants/

OpenStudy (e.mccormick):

Or are you useing printf? ``` #include <iostream> using namespace std; int main() { int a = 5; double b = 72.6; printf("%i%%\n%g%%\n\n",a,b); cin.sync(); cin.get(); return 0; } ``` http://www.cplusplus.com/reference/cstdio/printf/

OpenStudy (psymon):

Ah, okay, I see how you can get it to add it after now, cool. Didn't know you could do multiple, like << a << b << c << under one declared variable.

OpenStudy (e.mccormick):

Yah, the << dumps more to the stream. Many people, for clarity, do each line separately and use endl at the last spot. So: ``` cout << a << "%\n" << b << "%\n\n"; ``` becomes: ``` cout << a << "%" << endl; cout << b << "%" << endl << endl; ``` From an output standpoint there is no difference. Form a readability standpoint, that is much easier to read. From a typing standpoint it is more to type. So I suggest people stick with readable until they get comfortable, then for small messages switch to less typing because on small things it is easy to see mistakes.

OpenStudy (psymon):

Alright, cool. Yeah, I only know about doing it with endl; so wouldnt have known about the \n thing, lol.

OpenStudy (e.mccormick):

That site I linked, http://www.cplusplus.com , has good code samples and some basic tutorials. Other references to help you get started: Short, topical, funny videos: http://thenewboston.org/list.php?cat=16 There are some free beginners books here (and others): http://www.onlineprogrammingbooks.com/cplusplus/

OpenStudy (e.mccormick):

While the videos are a nice intro to commands, I strongly suggest people get a computer science based book. It helps steer people away from bad ideas, like fflush(stdin). (fflush is an OUTPUT buffer flush and it is undefined on the input, so it does not work the same in every compiler.)

OpenStudy (e.mccormick):

In fact, this one has a good reputation: http://greenteapress.com/thinkcpp/

OpenStudy (psymon):

Alrighty, thanks for the links and the suggestions, that helps a lot. I have a textbook, but lecture has done me more help than the book. Its just there are problems here and there that I guess lecture didnt go over or expects ya to pick up on. So any resources are awesome, definitely thanks :3

OpenStudy (e.mccormick):

Yah, the books can be hard to get until you know enough to understand what they are talking about! The videos, because they are listed by topic, can give you a quick way to see what the heck the book is talking about. That makes going back to the book easier.

OpenStudy (psymon):

Yeah, book hasnt done me much. But looks like I have what Ill need to be able to finish this assignment. Gotta get it all running asap, so Ill let ya go. Thanks again :3

OpenStudy (e.mccormick):

np. have fun!

OpenStudy (e.mccormick):

Oh, and lots of free boks out there. Like free math books: http://www.aimath.org/textbooks/textbooklist.html (since you seem to do a lot with math \(\ddot\smile\))

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!