Hello
I am glad to find this site. I have been searching for help with learning C++, to no avil. So my question, which is multi-fold: I have a book from 10 years back or so, ansi compliant, should it work with dev-cpp. Second, I learned the difference between char and values, but when I look at the code the book shows, it looks like they are entering only values (like 1). And when I try to code a simple program like choose number one or two, then output one of two texts. It pops up as a cmd prompt in xp sp2, and displays the opening message, but when I try to input something, then press enter, it exits(which isn't overly suprising) . But how do you input text (numbers in this case). And the reason the thing isn't working, in my opinion, is because the code is somewhat out dated. The book that I am using is Learn C++ in 21 days 2ed. .


Thanks
Vivo (newbie in training ;) )

C++ hasn't changed all that drastically to the point where legacy code wouldn't work.

Are you sure that your program isn't working? If you're using Visual C++, for example, if you try and run the program normally, it will exit right after successfully executing all statements. With your program, after it retrieves the input, it will likely execute the remainder of the statements so fast that you won't even see them. If you are using VC++, try running it without debugging and see what it gives you (or run it with debugging, setting a break point right before the program exits). If you're not running VC++, post up a bit of your code so that we have an idea what is going wrong.

By default, Dev-C++ uses the MinGW compiler which is a pretty good compiler. I used to use Dev, but the problem I had with it is that, yes the interface is nice, but many of it's subsystems are not very well implemented. I don't really recommend it. The debugger is not very good and the code complete tends to crash. Also, because its debugger isn't that great, if you don't somehow force your program to hold itself open it automatically closes when it finishes.

Right before your "return 0;" add the lines "cin.ignore();" and "cin.get();". That combination should hold the program open for you until you press the [ENTER] key again.

A couple others have posted recently about using the book "Learn C++ in 21 Days". I've never read it, but I'm not too sure I want to.

Share your code with us and we'll make some suggestions. Without it though, you really won't get much.

I pulled it down, but the forum rules require that you keep it on site. Please keep that in mind in the future.

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    const int Left=1;
    const int Right=2;
    
    int choice;
    cout << "Hello, choose a number.\n";
    cin >> choice ;
    if (Left )
    {
    cout <<" Congrats!\n";
}
    else;
    cout << "I lost the game.\n";
    return 0;
}

There are some minor formatting issues, follow this link for more information (courtesy of WaltP).

The one glaring thing that I see is that you haven't compared your input to any value. Currently, you're going to get " Congrats!\nI lost the game.\n" every time because your if is not written correctly. You just don't see it because it displays and the window closes so quickly.

You do not use semi-colons after conditional statements. When you do, you mess up the way the compiler interprets your statement(s) and you wind up with unexpected results.

This is what your if statement really should look like:

if (Left == choice)
{
  cout <<" Congrats!\n";
}
else
{
  cout << "I lost the game.\n";
}

See my previous post for how to hold open the window:

int main() {
  /* ... misc. code ... */

  cin.ignore();    //not always required, situation dependent
  cin.get();
  return 0;
}
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    const int Left=1;
    const int Right=2;
    
    int choice;
    cout << "Hello, choose a number.\n";
    cin >> choice ;
    if (Left==choice )
    {
    cout <<" Congrats!\n";
}
    else
{
    cout << "I lost the game.\n";
    return EXIT_SUCCESS;
}

// I remeber why I did not compare it to anything. I was trying to fix the code, but it was doing the same thing as it is now. And the only thing it will show is "hello, please choose a number". But when I go to enter a number, then press enter, the box closes. The reason I am trying dos is because I tried windows version, but it just closed on me.

Thank you

commented: You're a dense one... +0

:icon_rolleyes: Look at posts 2, 3, AND 5. You have already been told what the problem is THREE TIMES. Pay attention already.

And use [code] CODE tags [/code]...

sorry. It was a misunderstanding. I thought you wanted me to post the code (cin.ignore, cin.get) because you thought the program was just closing. Sorry about that. And I appreciate your time. (now to work on some text games)

Is this solved then?

yes thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.