Advertisement

C++ do while error

Started by March 18, 2015 10:17 AM
15 comments, last by MarkS_ 9 years, 10 months ago

Hi i have an issue with this block of code even though all my braces have been opened and closed. Can someone please help out? Regards

expected unqualified-id before 'do'
expected '}' at end of input
expected unqualified-id at end of input


#include<cstdlib>
#include<iostream>
#include<string>
using namespace std;

class President
{
    public:
           
        //int ca=0,cb=0,cc=0,tv=0;
        //string sa="Wamalwa",sb="Rotich",sc="Fatima";
        
        int ca,cb,cc,tv;
        string sa,sb,sc;
         char pchoice,pchoice2;
         
        
        void PWinner()
        {
            if (ca > cb && ca > cc)
               {
                  cout<< "and the winner is : Wamalwa" << endl;
               }
           else if (cb > ca && cb > cc)
               {
                  cout << "and the winner is : Rotich" << endl;
               }
           else if (cc > ca && cc > cb)
                  cout << "and the winner is : Fatima" << endl;
           else
                  cout << "no winning president go for a runoff" << endl;

        }

     //my error is here
      do
      {
        void PMenu()
        {
                  cout<<"\t Presidential Elections\n\n";
               cout<<"Candidates \n\n";
               cout<<"<A>Wamalwa \n";
               cout<<"<B>Rotich \n";
               cout<<"<C>Fatima \n\n";
               cout<<"Pick a candidate\n";
               cin>>pchoice;
        switch(pchoice)
         {
            case 'A':
            case 'a':
                ca++;
                break;
            case 'B':
            case 'b':
                cb++;
                break;
            case 'C':
            case 'c':
                cc++;
                break;
            
        }
       while(pchoice2=='V' || pchoice2=='v');
}
       if (pchoice2=='R' || pchoice2=='r')
       {
            cout<<"\t Chairman Elections\n\n";
            cout<<"Candidates:"<<"\t results:"<<"\n\n";
            cout<<"<A>Wamalwa : "<<ca<<"\n";
            cout<<"<B>Rotich : "<<cb<<"\n";
            cout<<"<C>Fatima : "<<cc<<"\n\n";
            tv=ca+cb+cc;
            cout<<"Total Votes : "<<tv<<"\n";
       }
    
    //private:
 
}
};
/*
class Senator
{
    public:
        //int pa=0,pb=0,pc=0,sv=0;
        //string xa="Mona", xb="Nyachae", xc="Nzuki";
        
        void SWinner()
        {
                
  //for senators
     if (pa > pb && pa > pc)
    {
        cout<< "and the winner is : roto" << endl;
    }
    else if (pb > pa && pb > pc)
    {
        cout << "and the winner is : maish" << endl;
    }
    else if (pc > pa && pc > pb)
    {
        cout << "and the winner is : maish" << endl;
    }
    else
        cout << "no winning senator go for a runoff" << endl;
            
        }
        
        void SMenu()
        {
                cout<<"\t senators Elections\n\n";
    cout<<"Candidates \n\n";
    cout<<"<D>Mona\n";
    cout<<"<E>Nyachae\n";
    cout<<"<F>Nzuki\n\n";
    cout<<"Enter your vote : ";
    cin>>choice;
    
    //for senators
    case 'D':
    case 'd':
        pa++;
        break;
    case 'E':
    case 'e':
        pb++;
        break;
    case 'F':
    case 'f':
        pc++;
        break;
        }
    cout<<"-----------------------\n";
    cout<<"Enter <v>vote <r> result and <q> quit : ";
    cin>>schoice2;
   // private:
};*/
int main()
{
    
   

    //for senaors
    //char Schoice, Sechoice;

  President Wamalwa;

  Wamalwa.PMenu();

  Wamalwa.PWinner();

//    do
//{



//    }

    
    //}
    
    /*///////senators
    cout<<"\t Secretary Elections\n\n";
    cout<<"Candidates \n\n";
    cout<<"<D>Mona"<<pa<<"\n";
    cout<<"<E>Nyachae "<<pb<<"\n";
    cout<<"<F>Nzuki "<<pc<<"\n";
    cout<<"Enter your vote : ";*/
    
    //sv=pa+pb+pc;
    
    //}

        
        
        system("pause");
        return 0;
}


You can't start the do-whole loop outside the function.

Advertisement

What function is that do-while loop supposed to be in? As it stands, it's in the class block for `President', but not in any function.

ph34r.png

You have close the PWinner function and after you have puts some other instruction. That's the error.

You have an executable statement at namespace level. That's just not a thing in C++.

Stephen M. Webb
Professional Free Software Developer

As stated you need the do in a function and also in that block you use 3 { including do and only close 2 } before while maybe you have acidentally put your function decleration in your do loop rather than your do loop in your function
Advertisement

I have to also question why your variable names are so utterly cryptic.

What would be the result of comparing a ca to a cc, do you know? I sure don't.

Thanks for the replies I have done the correction but i want it to display the PMenu until i press r || R which

will then print the results

This program was working without classes but i am now trying to use classes and that is why iam a bit stuck.

The original program without classes is below. You can see what iam trying to do with the current class arrangement.

Regards


#include<cstdlib>
#include<iostream>
#include<string>

#define PI = 3.142
using namespace std;



int main()
{
    system("COLOR CE"); 
    
	char choice,choice2;
	//for presidents
    int pa=0,pb=0,pc=0,pTotal=0;
	//string PName="Wamalwa";
	//for senators
	int sa=0,sb=0,sc=0,sTotal=0;
	//string SName="Mona";

        do
        {
        	cout<<"\t Presidential Elections\n\n";
        	cout<<"Candidates \n\n";
        	//add more presidential candidates here
        	cout<<"<A> Wamalwa \n";
        	cout<<"<B> Robert\n";
        	cout<<"\t senators Elections\n\n";
            cout<<"Candidates \n\n";
            //add more senatorial candidates here
        	cout<<"<D>Mona\n\n";
        	
        	cout<<"Enter your vote : ";
        	cin>>choice;
        	
       	 
	   switch(choice)
	       {
            	case 'A':
            	case 'a':
            		pa++;
             	break;
            	//use new case for other presidential candidates
            	
            	
            	//for senators
                case 'D':
            	case 'd':
            		sa++;
            		break;
            	//use new case for other senatorial candidates
       	   }

            	cout<<"-----------------------\n";
            	cout<<"Enter <v>vote <r> result and <q> quit : \a";
            	cin>>choice2;
            	 
        }
	while(choice2=='V' || choice2=='v');
	
	    ///
    	if (choice2=='R' || choice2=='r')
	        {
                //presidential results
            	cout<<"\t Presidential Elections\n\n";
            	cout<<"Candidates:"<<"\t results:"<<"\n\n";
            	cout<<"<A>Wamalwa : "<<pa<<"\a\n";
            
            	//_______senatorial results
                cout<<"\t Senatorial Elections\n\n";
            	cout<<"Candidates \n\n";
            	cout<<"<D>Mona: "<<sa<<"\n";
            	
            	pTotal=pa; //for other presidential canndidates add the variables
            	sTotal=sa;//for other senatorial canndidates add the variables
            	
            	//count total votes
            	cout<<"Total Presidential Votes cast : "<<pTotal<<"\n";
            	cout<<"Total Senatorial Votes cast : "<<sTotal<<"\n";
            	cout<<endl;
	        }
	     if (pa > pb && pa > pc)
        	{
        		cout<< " The presidential winner is : Wamalwa " << endl;
        	}
    //hint use else if for more candiates
  //for senators
         if (sa > sb && sa > sc)
    	    {
    		    cout<< " The senatorial winner is : Mona " << endl;
            }
	
	//hint use else if for more candiates
	
        
        system("pause");
        return 0;
}



Misspoke. Sorry about that.

While learning about using classes is good, usually classes are not used that way.

Classes are used to bundle together pieces of data or pieces of functionality. Usually a class is part of a bigger system, and the class implements a single responsibility.

In your code, you might use a class to represent details about a candidate.

For example:


class Candidate {
   ...
  public:
    std::string Name;
    int Votes = 0;   
}


Then in your code you can develop a system to work with any number of candidates.

Maybe extending it a little bit:


class Candidate {
   ...
  public:
    string Name;
    int Votes = 0;   

    void ReadName() { 
      cout << "\nEnter the candidate name:\n";
      getline( cin, Name);
    }

    void ReadVotes() {
      do {
        cout << "\nEnter votes for " << Name << "\n";
        if( !(cin >> Votes) && (Votes < 0) ) {
          cout << "Please enter a number between 0 and 2147483648\n";
          cin.clear();  // Clear any error flags
          cin.ignore(numeric_limits<streamsize>::max()); // Ignore any other values they typed
        }
      }
    }
...
}


Then in your code, you could work with a system of candidates. You could ask how many candidates you have, and make a collection with that many. Then you could loop through each candidate and prompt for names and votes. Then you can process the votes from the collection, and so on.

This topic is closed to new replies.

Advertisement