Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


C++ Data validation

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
zaczac
Master Cheater
Reputation: 0

Joined: 01 May 2007
Posts: 266
Location: Ontario, Canada

PostPosted: Fri Jan 27, 2012 11:06 am    Post subject: C++ Data validation Reply with quote

Just started learning C++ and got stuck while writting my code, the code works fine but if a user enters something other than a number it craps out.. if anyone could give me a hand I would appeciate it Shocked

Code:
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
   int score[5][3];
   int currentScoreAvg=0;
   int totalScore=0;
   int playerScore=0;
   for (int player = 0; player<5; player++)
   {
      cout << "Player "<<player+1<<endl;
      for(int game = 0; game<3; game++)
      {
         
         cout<< "Please Enter Score \n";
          cin>> playerScore;
         if(playerScore>300 || playerScore<0)
         {
            cout<<"Please enter a valid score between 1-300 inclusive \n";
            game--;
            
         }
         else
         {   
            score[player][game] = playerScore;
            currentScoreAvg += score[player][game];
         }
         }
         
      cout<< "Average score for this player is "<< currentScoreAvg/3<<endl;
      totalScore+=(currentScoreAvg/3);
      currentScoreAvg=0;
   }
   cout <<"Average score for whole team is " <<totalScore/5<<endl;
   system ("pause");
}
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Fri Jan 27, 2012 12:15 pm    Post subject: Reply with quote

You can use various functions such as:
- isdigit
- atoi
- stringstream (Read as string and output to a type of your choice.)
- scanf

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
zaczac
Master Cheater
Reputation: 0

Joined: 01 May 2007
Posts: 266
Location: Ontario, Canada

PostPosted: Fri Jan 27, 2012 12:45 pm    Post subject: Reply with quote

Tryed isdigit and isalpha but its still acting strange... see anything wrong with this?

Code:
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string>
using namespace std;

int main()
{
   int score[5][3];          //array to hold the score for each user and the current player
   int currentScoreAvg=0;    //variable to hold the users current average score
   int totalScore=0;         //variable to hold the total score (all users)
   char playerScore;        //variable to hold the input from the user (players Score)

   for (int player = 0; player<5; player++)
   {
      cout << "Player "<<player+1<<endl;
      for(int game = 0; game<3; game++)
      {
         cout<< "Please Enter Score \n";
          cin>> playerScore;

   
         if(playerScore>300 || playerScore<0 || isalpha(playerScore))
         {
            cout<<"Please enter a valid score between 1-300 inclusive \n";

            game --;
         }
         else
         {   
            score[player][game] = playerScore;
            currentScoreAvg += score[player][game];
         }
      }
      cout<< "Average score for this player is "<< currentScoreAvg/3<<endl;
      totalScore+=(currentScoreAvg/3);
      currentScoreAvg=0;
   }
   cout <<"Average score for whole team is " <<totalScore/5<<endl;
   system ("pause");
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites