Problem..

This is a discussion on Problem.. within the C++ Programming forums, part of the General Programming Boards category; I'm making a text based adventure game in C++. Sometimes it asks you to enter an integer, and if you ...

  1. #1
    Aki
    Aki is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Problem..

    I'm making a text based adventure game in C++. Sometimes it asks you to enter an integer, and if you enter a letter the game will get screwed up... so I was looking for a way to fix this and I found this:

    Code:
    int integer()
    { char inputdata[100];
    cin >> inputdata;
    return atoi(inputdata);
    }


    The only problem with this is that if you enter a character instead of a number, it will interpret it as 0, I just want it to say. "Invalid character" or something and ask you to input an actual number... anyway I can fix this?

    I was also wondering how I could make it save your game. All I need it to do is like save a bunch of variables to a text document, and then beable to read them when you open the game again. Can anyone help me??

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    For your integer/char problems: Newbie question (user error checking)

    For saving data...

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    void main()
    {
    	int numb;
    
    	ofstream fout;	// declare object that prints text into the file
    	ifstream fin;	// declare object that reads text from the file
    
    	fout.open("save.dat");	// Creating the save file
    
    	fout << 12;	// Print the number 12 into save.dat
    
    	fout.close();	// Close the file
    
    	fin.open("save.dat");	// Opening the save file
    
    	fin >> numb;	// Read the value of numb
    
    	fin.close();               // Close the file
    
    	cout << numb << endl;	// numb would be 12
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Aki
    Aki is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    hrm.. still having problems

    Code:
     
    int integer() 
    { char inputdata[100]; 
      cin >> inputdata; 
      return atoi(inputdata); 
    } 
    
    int choice(int &hp, int &hpmax, int &mp, int &mpmax, int &atk, int &def, int &pts) 
    { int temp; char ans; 
    
      do{ 
        cout << "     You have " << pts << " points" << 
        endl << "     Attack: "; 
        temp = integer(); 
        cout << endl; 
    
        if (temp > pts) 
        { cout << "     Not enough points." << endl; 
          temp = 0; 
        } 
      }while(temp > pts);

    Here's what I tried:

    Code:
     
    using namespace std; 
    int choice(int &hp, int &hpmax, int &mp, int &mpmax, int &atk, int &def, int &pts) 
    { int temp; char ans; 
    
      do{ 
        cout << "     You have " << pts << " points" << 
        endl << "     Attack: "; 
        temp = integer(); 
        cout << endl; 
    
        if ((temp > pts) || (!cin)) 
        { cout << "     Not enough points." << endl; 
          temp = 0; 
        } 
      }while((temp > pts) || (!cin));

    This just gives me an error and loops forever.
    Again, the problem with the way I did it, is that if I enter a letter it will assume it is 0. Any idea on how i fix?

  4. #4
    Aki
    Aki is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    anyone help?

  5. #5
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    There is a header, I think it's called <ctype.h> and it contains checking functions one of them is isAlpha() for checking whether input is an Alphabet letter and there others to check to see if it is Numerical or both.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: Problem..

    Originally posted by Aki
    I'm making a text based adventure game in C++. Sometimes it asks you to enter an integer, and if you enter a letter the game will get screwed up... so I was looking for a way to fix this and I found this:

    Code:
    int integer()
    { char inputdata[100];
    cin >> inputdata;
    return atoi(inputdata);
    }


    The only problem with this is that if you enter a character instead of a number, it will interpret it as 0, I just want it to say. "Invalid character" or something and ask you to input an actual number... anyway I can fix this?
    Here's what I'd do. Prompt for an integer, but store what the user enters into an array of characters. Then, search through the array looking for digits. You can use this:
    Code:
    #include <ctype.h>
    
    int isdigit(int ch);
    That function will take in one character and return 1 if it is a digit 0-9 or 0 if it is not. Take those digits and store them in a seperate array of integers. Remember type casting problems. If you use my method you'll be going from chars to ints so whip out a table of ASCII characters and figure out how to convert from characters to integers for digits. Then, convert that array of single digit integers to an intónot tough to do with base ten numbers. You can use a while or do loop to continue this process if invalid data is entered.

    Oh, wait, I think I misinterpreted what you asked for. If you want, you could stop looking through the array of chars at the first instance of something other than a digit in order to display an error message and prompt the user to enter valid data. Anyway, same methods. I hope all of this helped. Happy coding :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21