New to C++ (simple code confusion)

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
Yotta2r
DCEmu Newbie
DCEmu Newbie
Posts: 7
Joined: Tue Jun 14, 2011 11:04 am
Has liked: 0
Been liked: 0

New to C++ (simple code confusion)

Post by Yotta2r » Thu Jun 16, 2011 2:32 pm

Hi :)

I'm completely new to C++, and I found this forum.. I'm sorry if I'm not allowed to post here..

I'm following the book "beginning c++ game programming" by Michael Dawson, and I'm on page 14/ 15.

On those pages there are some codes, but they simply wont compile & run.. I get an error (red mark). I use Dev-C++.

I'm really sure that the code isn't wrong. I have even checked it up with a few sites..

Here is the code:

Code: Select all

// Game Stats
// Demonstrates declaring and initializing variables

#include <iostream>
using namespace std;

int main()
{
    int score;
    double distance;
    char playAgain;
    bool shieldsUp;

    short lives, aliensKilled;

    score = 0;
    distance = 1200.76;
    playAgain = `y';
    shieldsUp = true;
    lives = 3;

    aliensKilled = 10;

    double engineTemp = 6572.89;

    cout << "\nscore: " << score << endl;
    cout << "distance: " << distance << endl;
    cout << "playAgain: " << playAgain << endl;
    //skipping shieldsUp since you don't generally print Boolean values
    cout << "lives: " << lives << endl;
    cout << "aliensKilled: "<< aliensKilled << endl;
    cout << "engineTemp: " << engineTemp << endl;

    int fuel;
    cout << "\nHow much fuel? ";
    cin >> fuel;
    cout << "fuel: " << fuel << endl;

    typedef unsigned short int ushort;
    ushort bonus = 10;
    cout >> "\nbonus: " << bonus << endl;

    return 0;
}

Can someone tell me whats wrong? :O


- Thanks! :)
Yotta2r
DCEmu Newbie
DCEmu Newbie
Posts: 7
Joined: Tue Jun 14, 2011 11:04 am
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by Yotta2r » Thu Jun 16, 2011 3:30 pm

The errors occur from:

Code: Select all

    cout >> "\nbonus: " << bonus << endl;
and

Code: Select all

    playAgain = `y';

....?
Chilly Willy
DC Developer
DC Developer
Posts: 414
Joined: Thu Aug 20, 2009 11:00 am
Has liked: 0
Been liked: 2 times

Re: New to C++ (simple code confusion)

Post by Chilly Willy » Thu Jun 16, 2011 5:19 pm

Consoles rarely every support "standard" out or in, so things like printf and cout can't be used. Also, no programming language out supports "smart" quotes, be they single or double. Use a programmer's editor instead of a text editor for coding. Visual Studio on Windows, Bloodshed, CodeBlocks, and Geany are all good IDEs with syntax coloring. Eclipse is also used by some folks, but a little heavy for my taste. I prefer Geany for my uses.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5387
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: New to C++ (simple code confusion)

Post by BlueCrab » Thu Jun 16, 2011 6:14 pm

Yotta2r wrote:

Code: Select all

    cout >> "\nbonus: " << bonus << endl;
You've got the first stream operator there going the wrong direction. Change the ">>" at the beginning of the line to "<<".

Code: Select all

    playAgain = `y';
You have to use the ' character for both sides. You've got a ` before the y, which is wrong.
Yotta2r
DCEmu Newbie
DCEmu Newbie
Posts: 7
Joined: Tue Jun 14, 2011 11:04 am
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by Yotta2r » Fri Jun 17, 2011 7:19 am

Chilly Willy wrote:Consoles rarely every support "standard" out or in, so things like printf and cout can't be used. Also, no programming language out supports "smart" quotes, be they single or double. Use a programmer's editor instead of a text editor for coding. Visual Studio on Windows, Bloodshed, CodeBlocks, and Geany are all good IDEs with syntax coloring. Eclipse is also used by some folks, but a little heavy for my taste. I prefer Geany for my uses.
I seriously didnt understand a thing of that! Im currently too noob, haha! But really, Thanks anyway! :D
Yotta2r
DCEmu Newbie
DCEmu Newbie
Posts: 7
Joined: Tue Jun 14, 2011 11:04 am
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by Yotta2r » Fri Jun 17, 2011 7:19 am

BlueCrab wrote:
Yotta2r wrote:

Code: Select all

    cout >> "\nbonus: " << bonus << endl;
You've got the first stream operator there going the wrong direction. Change the ">>" at the beginning of the line to "<<".

Code: Select all

    playAgain = `y';
You have to use the ' character for both sides. You've got a ` before the y, which is wrong.

That worked! Thanks A LOT! :D
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by PH3NOM » Fri Jun 17, 2011 9:39 am

Yotta2r wrote:
Chilly Willy wrote:Consoles rarely every support "standard" out or in, so things like printf and cout can't be used. Also, no programming language out supports "smart" quotes, be they single or double. Use a programmer's editor instead of a text editor for coding. Visual Studio on Windows, Bloodshed, CodeBlocks, and Geany are all good IDEs with syntax coloring. Eclipse is also used by some folks, but a little heavy for my taste. I prefer Geany for my uses.
I seriously didnt understand a thing of that! Im currently too noob, haha! But really, Thanks anyway! :D
Your entire program prints out a few lines, and has the user type in some input.

It does all video output and user input via cin/cout.

Sure, you can get it to compile, but problem is, this will not work like you expect it to on Dreamcast.

Simply, on Dreamcast, cout will not print to the screen, and cin will not get the user input from a keyboard.
Yotta2r
DCEmu Newbie
DCEmu Newbie
Posts: 7
Joined: Tue Jun 14, 2011 11:04 am
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by Yotta2r » Fri Jun 17, 2011 2:41 pm

PH3NOM wrote:
Yotta2r wrote:
Chilly Willy wrote:Consoles rarely every support "standard" out or in, so things like printf and cout can't be used. Also, no programming language out supports "smart" quotes, be they single or double. Use a programmer's editor instead of a text editor for coding. Visual Studio on Windows, Bloodshed, CodeBlocks, and Geany are all good IDEs with syntax coloring. Eclipse is also used by some folks, but a little heavy for my taste. I prefer Geany for my uses.
I seriously didnt understand a thing of that! Im currently too noob, haha! But really, Thanks anyway! :D
Your entire program prints out a few lines, and has the user type in some input.

It does all video output and user input via cin/cout.

Sure, you can get it to compile, but problem is, this will not work like you expect it to on Dreamcast.

Simply, on Dreamcast, cout will not print to the screen, and cin will not get the user input from a keyboard.
Ahh.. Well... The whole book is using this program, so I will have to use that program for now! Thanks for explanation! And thanks for the warning! :)
Ex-Cyber
DCEmu User with No Life
DCEmu User with No Life
Posts: 3641
Joined: Sat Feb 16, 2002 1:55 pm
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by Ex-Cyber » Fri Jun 17, 2011 4:25 pm

Yotta2r wrote:Ahh.. Well... The whole book is using this program, so I will have to use that program for now!
That's normal. C++ itself does not specify any sophisticated I/O framework, just the bare minimum for writing usable programs (console I/O and file I/O). There are many different ideas and standards for how to do things like graphics, sound, and GUIs, so the C++ standard doesn't try to choose these things for you. C++ doesn't stop you from doing them, but it also doesn't say how to do them, so a "C++" book won't really cover them. When you're ready for those things, you'll need to choose libraries for them and learn how to use those libraries.
I seriously didnt understand a thing of that!
He's worried that you might be using a word processing program (like Word) to write your code instead of a text editor or IDE, because the quote problem you had looks like something that word processing programs do to "correct" your punctuation.
"You know, I have a great, wonderful, really original method of teaching antitrust law, and it kept 80 percent of the students awake. They learned things. It was fabulous." -- Justice Stephen Breyer
Yotta2r
DCEmu Newbie
DCEmu Newbie
Posts: 7
Joined: Tue Jun 14, 2011 11:04 am
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by Yotta2r » Sat Jun 18, 2011 6:25 am

Ex-Cyber wrote:
Yotta2r wrote:Ahh.. Well... The whole book is using this program, so I will have to use that program for now!
That's normal. C++ itself does not specify any sophisticated I/O framework, just the bare minimum for writing usable programs (console I/O and file I/O). There are many different ideas and standards for how to do things like graphics, sound, and GUIs, so the C++ standard doesn't try to choose these things for you. C++ doesn't stop you from doing them, but it also doesn't say how to do them, so a "C++" book won't really cover them. When you're ready for those things, you'll need to choose libraries for them and learn how to use those libraries.
I seriously didnt understand a thing of that!
He's worried that you might be using a word processing program (like Word) to write your code instead of a text editor or IDE, because the quote problem you had looks like something that word processing programs do to "correct" your punctuation.
1. Thanks for clearing that up for me! really nice beginner information! Should serve me well :)

2. I'm only typing in with notepad and dev-c++, but thanks to both of you! :D
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 353
Joined: Wed Mar 30, 2011 12:13 pm
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by RyoDC » Sat Jun 18, 2011 3:45 pm

Damn, I don't understand. Why when some idiot starts a thread, it get a tons of answers. When I start a thread, I receive nothing. Why?
How do I try to build a Dreamcast toolchain:
Image
Yotta2r
DCEmu Newbie
DCEmu Newbie
Posts: 7
Joined: Tue Jun 14, 2011 11:04 am
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by Yotta2r » Sat Jun 18, 2011 3:47 pm

RyoDC wrote:Damn, I don't understand. Why when some idiot starts a thread, it get a tons of answers. When I start a thread, I receive nothing. Why?
The secret is the thanks and the smiley's :)
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 353
Joined: Wed Mar 30, 2011 12:13 pm
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by RyoDC » Sat Jun 18, 2011 4:24 pm

Yeah, quiet it is.
How do I try to build a Dreamcast toolchain:
Image
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5387
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: New to C++ (simple code confusion)

Post by BlueCrab » Sat Jun 18, 2011 5:26 pm

RyoDC wrote:Damn, I don't understand. Why when some idiot starts a thread, it get a tons of answers. When I start a thread, I receive nothing. Why?
The key is the fact that people knew the answer to the question posed in this thread. General C/C++ questions are usually pretty easy. Ask about specific IDEs and such, and then you limit your group of potential responders. For instance, I've never used Code::Blocks (or Dev-C++ in a very long while, for that matter). I tend to ignore questions directly about either one of them, since I have no experience with how they work or fit into the environment as a whole. I can't speak for others around here, but I'd assume most people don't bother replying to threads where they have nothing to add.

Also, calling the guy who opened this thread an idiot is rude, and not a way to get people to answer your questions either. Being rude will only make people want to ignore you (or be rude right back). Being nice goes a long way.
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 353
Joined: Wed Mar 30, 2011 12:13 pm
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by RyoDC » Sun Jun 19, 2011 5:31 am

Well, that was not an insult, don't take me wrong. Idiot Was just a common idiom. You can replace it by the 'random guy'. How can someone could be offended by something that is devoid of facts? I've just used the first word that came to my head.
Well, anyway, my reputation is stained now. (And, I don't like when people begin to behave like boring asses, I'll be better quit then would continue to talk to such guys. I think you all must be more opened to people and to all new).
Sorry if I offended someone. Didn't wanna to do that, really.
How do I try to build a Dreamcast toolchain:
Image
OneThirty8
Damn Dirty Ape
Damn Dirty Ape
Posts: 5031
Joined: Thu Nov 07, 2002 11:11 pm
Location: Saugerties, NY
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by OneThirty8 » Mon Jul 04, 2011 4:47 pm

RyoDC wrote:Well, that was not an insult, don't take me wrong. Idiot Was just a common idiom.
Every native speaker of English that I know would be offended if referred to as an idiot.
User avatar
Neoblast
DC Developer
DC Developer
Posts: 312
Joined: Sat Dec 01, 2007 8:51 am
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by Neoblast » Mon Jul 04, 2011 7:50 pm

RyoDC wrote:Damn, I don't understand. Why when some idiot starts a thread, it get a tons of answers. When I start a thread, I receive nothing. Why?
Man, that is so not nice.
No wonder why you don't get replies.
This forum is to help people with programming issues.
We're open to new people but insulting is not the way to go...
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 353
Joined: Wed Mar 30, 2011 12:13 pm
Has liked: 0
Been liked: 0

Re: New to C++ (simple code confusion)

Post by RyoDC » Tue Jul 05, 2011 2:59 pm

Again, I'm sorry for such inconvenience. I've changed my way of dealing.
How do I try to build a Dreamcast toolchain:
Image
Post Reply