Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include<iostream> #include<cmath> #include<cstdlib> #include<fstream> #include

ID: 3565913 • Letter: #

Question

 #include<iostream> 
 #include<cmath> 
 #include<cstdlib> 
 #include<fstream> 
 #include <iomanip> 
 #include <ctype.h> 
 #include <string.h> 
 #include <stdio.h> 
 using namespace std; 
    
 char name[50]; 
 int totalCorrect=0,totalWrong=0; 
 double earning=0.0; 
 bool credits(); 
 bool checkUserAnswer(double answer,double result); 
 char menu(); 
 void updateStats(bool correct); 
 void displayStats(); 
 double generateAddition(double a,double b); 
 double generateSubstraction(double a,double b); 
 double generateMultiplication(double a,double b); 
 double generateDivision(double a,double b); 
 void saveStats(); 
 void retireveStats(); 
 int main() 
 { 
    
            char ch; 
    
            if(credits()) 
            { 
    
                while(true) 
                { 
                     ch = menu(); 
                     if(ch=='n'||ch=='N') 
                     break; 
                } 
    
            } 
            else 
            { 
                cout<<"See you next time! "; 
            } 
    return 0; 
 } 
    
 bool credits() 
 { 
            char ch; 
            cout<<"*************************** "; 
            cout<<"*************************** "; 
            cout<<"*************************** "; 
            cout<<"*******             ******* "; 
            cout<<"******* TheMathGame ******* "; 
            cout<<"*******     By      ******* "; 
            cout<<"*******    Kurou    ******* "; 
            cout<<"*******             ******* "; 
            cout<<"*************************** "; 
            cout<<"*************************** "; 
            cout<<"*************************** "; 
            cout<<" "; 
            cout<<"y/Y to continue , or any other char to exit "; 
            cin>>ch; 
            system("cls"); 
            if(ch=='y'||ch=='Y') 
            { 
                cout<<"Enter your name and press <ENTER> "; 
                cin>>name; 
                cin.getline(name,50); 
                for(int i=0; i< strlen(name); ++i) 
                { 
                    if(!isalpha(name[i])) 
                 { 
                        cin.clear(); 
                                       cin.ignore(100000,' '); 
                                       cout << "That is not a name, please enter a name! "; 
                                       cin>>name; 
                                       cin.getline(name,50); 
                                       i = -1; 
                    } 
                } 
                retireveStats(); 
                system("cls"); 
                return true; 
            } 
            else 
            { 
                system("cls"); 
                return false; 
            } 
    
 } 
    
 char menu() 
 { 
         char ch; 
         bool correct; 
         cout<<"*****CHOOSE A PROBLEM****** "; 
         cout<<"*************************** "; 
         cout<<"*************************** "; 
         cout<<"******              ******* "; 
         cout<<"****** 1. ADD       ******* "; 
         cout<<"****** 2. SUBSTRACT ******* "; 
         cout<<"****** 3. MULTIPLY  ******* "; 
         cout<<"****** 4. DIVIDE    ******* "; 
         cout<<"****** 5. STATS     ******* "; 
         cout<<"****** n/N to QUIT  ******* "; 
         cout<<"******              ******* "; 
         cout<<"*************************** "; 
         cout<<"*************************** "; 
         cin>>ch; 
         system("cls"); 
    
         int a; 
         int b; 
    
         do 
         { 
             a = rand() % 10+1; 
             b = rand() % 10+1; 
         } 
         while(a<b); 
    
         double result,answer; 
         do 
         { 
             switch(ch) 
             { 
    
                            case '1': 
                                    result = a+b; 
                                    answer = generateAddition(a,b); 
                                    correct=checkUserAnswer(answer,result); 
                                    updateStats(correct); 
                                    break; 
    
                            case '2': 
                                    result = a-b; 
                                    answer=generateSubstraction(a,b); 
                                    correct=checkUserAnswer(answer,result); 
                                    updateStats(correct); 
                                    break; 
                            case '3': 
                                    result = a*b; 
                                    answer=generateMultiplication(a,b); 
                                    correct=checkUserAnswer(answer,result); 
                                    updateStats(correct); 
                                    break; 
    
                            case '4': 
                                    result = a/b; 
                                    answer=generateDivision(a,b); 
                                    correct=checkUserAnswer(answer,result); 
                                    updateStats(correct); 
                                    break; 
                            case '5': 
                                    displayStats(); 
                                    system("cls"); 
                                    break; 
                            case 'n': 
                                    saveStats(); 
                                    cout<<"See you next time! "; 
                                    return ch; 
                            case 'N': 
                                    saveStats(); 
                                    cout<<"See you next time! "; 
                                    return ch; 
                            default: 
                                    cout<<"That's not a valid option, please enter an option! "; 
                                    cout<<" "; 
                        } 
    
    
                    } 
         while(ch=='n'||ch=='N'); 
    
    
     return 0; 
 } 
    
 bool checkUserAnswer(double answer,double result) 
 { 
     if(answer == result) 
     { 
         cout<<"**********RIGHT!*********** "; 
         cout<<" "; 
         return true; 
     } 
     else 
     { 
         cout<<"**********WRONG!*********** "; 
         cout<<" "; 
         return false; 
     } 
 } 
    
 void updateStats(bool correct) 
 { 
     if(correct) 
     { 
         totalCorrect++; 
         earning = earning + 0.05; 
     } else { 
         totalWrong++; 
         earning = earning - 0.03; 
     } 
 } 
    
 void displayStats() 
 { 
     system("cls"); 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cout<<"*****       "<<name<<"     ***** "; 
     cout.precision(2); 
     cout<<"***** Total Earnings "<< fixed <<earning<<" * "; 
     cout<<"***** Total Correct "<<totalCorrect<<" ***** "; 
     cout<<"***** Total Wrong "<<totalWrong<<"   ***** "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cout<<" "; 
    
     system("pause"); 
     system("cls"); 
 } 
    
 double generateAddition(double a,double b) 
 { 
     double answer; 
     cout.precision(0); 
     cout<<"*********ADDITION********** "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cout<<"******** "<<a<<" + "<<b<< " =? ********* "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cin>>answer; 
    
     while (!cin.good()) 
     { 
         cin.clear(); 
         cin.ignore(100000, ' '); 
         cout<<"That's not a number, please enter a number! "; 
         cin>>answer; 
     } 
     system("cls"); 
     return answer; 
 } 
    
 double generateSubstraction(double a,double b) 
 { 
     double answer; 
     cout.precision(0); 
     cout<<"*******SUBSTRACTION******** "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cout<<"******** "<<a<<" - "<<b<< " =? ********* "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cin>>answer; 
    
     while (!cin.good()) 
     { 
         cin.clear(); 
         cin.ignore(100000, ' '); 
         cout<<"That's not a number, please enter a number! "; 
         cin>>answer; 
     } 
     system("cls"); 
     return answer; 
 } 
    
 double generateMultiplication(double a,double b) 
 { 
     double answer; 
     cout.precision(0); 
     cout<<"*****MULTIPLICATION******** "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cout<<"******** "<<a<<" * "<<b<< " =? ********** "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cin>>answer; 
    
     while (!cin.good()) 
     { 
         cin.clear(); 
         cin.ignore(100000, ' '); 
         cout<<"That's not a number, please enter a number! "; 
         cin>>answer; 
     } 
     system("cls"); 
     return answer; 
 } 
    
 double generateDivision(double a,double b) 
 { 
     double answer; 
     cout.precision(0); 
     cout<<"*********DIVISION********** "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cout<<"******** "<<a<<" / "<<b<< " =? ********* "; 
     cout<<"*************************** "; 
     cout<<"*************************** "; 
     cin>>answer; 
    
     while (!cin.good()) 
     { 
         cin.clear(); 
         cin.ignore(100000, ' '); 
         cout<<"That's not a number, please enter a number! "; 
         cin>>answer; 
     } 
     system("cls"); 
     return answer; 
 } 
    
 void saveStats() 
 { 
     string filename(name); 
     filename += ".txt"; 
    
      ofstream outData; 
      outData.open(filename.c_str()); 
      //outData << "Writing this to a file. "; 
      //outData<<"*************************** "; 
      //outData<<"*************************** "; 
      //outData<<"*************************** "; 
      outData<<name<<" "; 
      outData<<earning<<" "; 
      outData<<totalCorrect<<" "; 
      outData<<totalWrong<<" "; 
      //outData<<"*************************** "; 
      //outData<<"*************************** "; 
      //outData<<"*************************** "; 
      outData.close(); 
 } 
    
 void retireveStats() 
 { 
     string filename(name); 
     filename += ".txt"; 
     ifstream getfile; 
     getfile.open(filename.c_str()); 
     getfile >> name; 
     getfile >> earning; 
     getfile >> totalCorrect; 
     getfile >> totalWrong; 
     getfile.close(); 
    
 } 
    
    
 This is my program, I want this program can validate cin>>name. The name I entered can't have a space, a number or a symbol, only letters. I don't know how to do that, please revise my program. Also, after my cin.getline(name, 50), I can't return value to char name[50]. How to revise it? 

Explanation / Answer

Following function check the given conditions for name

bool Validate(char[] name)
{
   for (int i = 0; name[i] != ''; i++)
   {
       if (!((name[i] >= 65 && name[i] <= 90) || (name[i] >= 97 && name[i] <= 122)))//then only name has only alphabets(comparing ASCII values)
           return 0;
   }
   return 1;
}