problems with Dev C++

Pages: 12
I recommend you actually LEARN the language before attempting to make a game. Here is an example of using cin.ignore() to pause.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <limits>
using std::numeric_limits;
using std::streamsize;

int main()
{
   cout << "Hello, world!" << endl;
   cout << "Press Enter to continue...";
   cin.ignore(numeric_limits<streamsize>::max(), '\n');
   return 0;
}


What this does is ignore either the maximum amount of characters that can be input on screen or wait for a newline (hence, the Enter key).

Also, do NOT use the entire STD namespace... learn to use only specific parts of it, like in the above example.
Last edited on
Also... here, I've modified your code for you. Make sure you actually read it and understand it, rather than just compiling it and moving on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <limits>
    
int main()
{
    int Blacksmith = 1;
    int MagicUser = 2;
    int Thief = 3;
    int Warrior = 4;
    int choose;
    
   cout << "\n \n \t Your adventure starts in a little town called Garkos's Ferry.\n";
   cout << " You were born in this town and was raised as a:\n";
   cout << "\n 1. Blacksmith \n 2. Magic User \n 3. Thief \n 4. Warrior\n";
   cout << "Make your choice: ";

   cin >> choose;
   
   switch (choose) 
  {
  case 1:
    cout << "You have selected Blacksmith!\n";
   break;
   case 2:
    cout << "You have selected Magic User!\n";
   break;
   case 3:
    cout << "You have selected Thief!\n";
   break;
   case 4:
    cout << "You have selected Warrior!\n";
   break;
  default:
    cout << "Invalid Choice!\n";
  }
    cout << "Press Enter to continue...";
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}


I noticed that you declared the character classes as global variables and then re-declared them in main? What the hell did you do that for? You can't declare something twice. Also, don't use global variables.

Go read a book.
Last edited on
Thanks for taking the time to show me. Just to clear up a few things: I am taking a c++ course in school this summer and I went and got my text material early.

I was hoping to gather a better understanding of the language and my text said best was is to attempt different things and see what the code does.

That is kinda where I am now. I am not really making a game per say...just a little fantasy thing I thought that could interest me while I learn different things about the language.

As far as the declaring them 2 times, I knew not to do that..lol I don't know why I did so thanks for pointing that out. I did have a couple questions:

#1. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); What does this do? I seen what you said it did but I don't fully understand. Does it mean I can type as many chars as I want forever?

#2. I noticed it still doesn't recognize the number they type and display the text. Why is that?

Again, I hope not to frustrate you with my questions, I am not nor have ever been a very fast learner, I am just hoping to understand..so I hope my questions don't sound redundant.

Thanks for your time.
It was my bad. cin.ignore is very picky and doesn't work in certain situations. cin.get and cin.ignore both don't seem to work after taking input through cin. I think you have to flush the buffer, but you're new, so I don't want to confuse you.

Don't get in the habit of doing this, but until I get back to you on a better way to pause, use this...

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <conio.h>

int main()
{
    cout << "Hello, world!" << endl;
    cout << "Press Enter to continue...";
    _getch();
    return 0;
}


I say not to get in the habit of this because "conio.h" is not portable.

As far as the cin.ignore command goes, let me break it down for you.

cin.ignore() = Command used to ignore the characters specified until the second parameter is input. For example:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
    cout << "Hello, world!";
    cin.ignore(50, '\n');
    return 0;
}


This will print "Hello, world!" and then cin.ignore will ignore the next 50 characters that are input or wait for a newline character (pressing the Enter key.)

numeric_limits<streamsize>::max() will return the value of the maximum amount of characters that the input stream can hold (someone correct me on this if I'm wrong)

So... cin.ignore(numeric_limits<streamsize>::max(), '\n'); will ignore the maximum amount of characters you can input into the input buffer, or wait for a newline character. This is supposed to be a very good way to pause the console, but like cin.get, if you have previously input data into the input buffer, it will not work. You need to clear the buffer (I'll post some code later)

Alternatively, you could just run your programs from the command line.

You're not frustrating me. Just don't become one of those people who asks the forum members to do his homework assignments for him, if you know what I mean.
Last edited on
Here, I wrote a custom pause function for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using std::cout;
using std::cin;
using std::getline;
using std::endl;
#include <limits>
using std::numeric_limits;
using std::streamsize;
#include <cstring>
using std::string;

void pause();

int main()
{
    int age;
    string name;
    cout << "What is your name? ";
    getline(cin, name);
    cout << "How old are you? ";
    cin >> age;
    
    cout << name << " is " << age << " years old!" << endl;
    
    pause();
    return 0;
}

void pause()
{
    cin.clear();
    cin.ignore(1, '\n');
    cout << "Press Enter to continue . . ." << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');   
}
Okay, thanks again with your helpful post. The more and more I look at this makes me realize how I really need to go back to the basics again until the code starts to make more since to me.

Right now my knowledge of c++ is simply adding iostream, namespace, and cout. I have a basic understanding of if statments, but I need to learn more.

Do you now any good tutorials that explain things in depth? I have been googling and I have found some, but most of them I can't even understand the explanations because in some cases they are harder to understand than the code itself..heh. Thanks again.
Topic archived. No new replies allowed.
Pages: 12