5 Replies - 317 Views - Last Post: 30 July 2014 - 02:00 AM Rate Topic: -----

#1 Eddieteo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-July 14

Anyone know how to use alphabets to break a do while loop? UEGENT!

Posted 29 July 2014 - 10:54 PM

#include<iostream>
using namespace std;
int main()
{
	int choice[10] = { 0 };
	int vote, highvote;
	
	
		cout << "1 Lional Messi ";
		cout << "\n2 Argen Roben ";
		cout << "\n3 Thomas Miller ";
		cout << "\n4 James Roduguez ";
		cout << "\n5 Neymar Da Silva Santos junior";
		cout << "\n6 Christiana Ronaldo ";
		cout << "\n7 Jan Vertonghen ";
		cout << "\n8 Robin van Persie";
		cout << "\n9 Miralem Pjanic";
		cout << "\n10 Karim Mostafa Benzema\n";

		do
		{
			
				cout << "your choice is :";
				cin >> vote;
				if (vote > 0 && vote <= 10)
					choice[vote - 1] += 1;
				else
					break;
		
		}
		while (vote > 0 && vote <= 10);
		

	

	highvote = 0;

	for (int i = 0; i < 10; i++)
	{
		if (choice[i]>highvote)
			highvote = choice[i];
	}

	for (int i = 0; i < 5; i++)
	{
		cout << "\nThe best 5 vote for " << i + 1 << " is : " << choice[i] << "\n" << endl;
	}
		system("pause");
}


My problem is i have to stop the loop when i enter any alphabets, i tried a lot of method but still doesnt work.
Help please!

I havent finish my code, just wanted to solve that particular stuff first..

Is This A Good Question/Topic? 0
  • +

Replies To: Anyone know how to use alphabets to break a do while loop? UEGENT!

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3565
  • View blog
  • Posts: 11,074
  • Joined: 05-May 12

Re: Anyone know how to use alphabets to break a do while loop? UEGENT!

Posted 29 July 2014 - 10:58 PM

You can check to see if a stream is in an error state after you try to extract something from the stream.
Was This Post Helpful? 0
  • +
  • -

#3 Eddieteo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-July 14

Re: Anyone know how to use alphabets to break a do while loop? UEGENT!

Posted 29 July 2014 - 11:03 PM

What does it mean by stream? sorry, i'm a beginner for C++..
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3565
  • View blog
  • Posts: 11,074
  • Joined: 05-May 12

Re: Anyone know how to use alphabets to break a do while loop? UEGENT!

Posted 29 July 2014 - 11:06 PM

Personally, I would focus first on getting the major functionality of your code first, and then deal with bad user input (malicious or otherwise) after I've got a working program.

The cout and cin objects are output and input streams, respectively. Your book or tutorial should have at the very least mentioned that.
Was This Post Helpful? 0
  • +
  • -

#5 Eddieteo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-July 14

Re: Anyone know how to use alphabets to break a do while loop? UEGENT!

Posted 29 July 2014 - 11:33 PM

ok i will try myself first, thanks!
Was This Post Helpful? 0
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Anyone know how to use alphabets to break a do while loop? UEGENT!

Posted 30 July 2014 - 02:00 AM

If you mean 'how to break out of an input loop, using cin to input integers, if a non-integer was entered' ... ?

// ...
string prompt = "Enter an int (or enter 'x' to exit input loop) : ";
cout << prompt;
int tmp;
while( cin >> tmp )
{
   cout << "You entered: " << tmp << endl;
   cout << prompt <<;
}

cin.clear(); // clear cin error state flags
cin.sync(); // 'flush' cin stream ...
// ..
.


But here, I don't think that is what you want to do.

It is probably a good idea to use a container to hold the names.
That way changes to the names and the number of names can easily be accommodated ...
like this:


const string CHOICES[] =
    {
        "Lional Messi",
        "Argen Roben",
        "Thomas Miller",
        "James Roduguez",
        "Neymar Da Silva Santos junior",
        "Christiana Ronaldo",
        "Jan Vertonghen",
        "Robin van Persie",
        "Miralem Pjanic",
        "Karim Mostafa Benzema"
    } ;
const int NUM_CHOICES = sizeof CHOICES / sizeof *CHOICES;

void showChoices()
{
    for( int i = 0; i < NUM_CHOICES; ++ i )
         cout << setw( 3 ) << (i+1) << ": " << CHOICES[i] << endl;
}



Now if you want to print out the top names with the most votes ...
sorting an array of pairs holding an index and count_of_votes
could be used to facilitate that:

// used to help sort in decreasing 'count' order ...
struct MyPair
{
    // ctor...
    MyPair() : index(0), count(0) {}

    int index;
    int count;
} ;

// bubble sort into descending count order ... 
// Note: bubble sort is ok to use for *small* sized arrays
// function prototype ... 
// Note: you WILL need to define this bubble sort before it is called
void bsort( MyPair ary[], int size );




Now ... to facilitate getting input and prompting for input,
you may like to add these utilities to your 'student tool kit' ...


// some utilities that could be used here ...

string takeInString( const string& msg = "" )
{
    cout << msg << flush;
    string val;
    getline( cin, val );
    return val;
}
char takeInChr( const std::string& msg = "" )
{
    string reply = takeInString( msg );
    if( reply.size() )
		return reply[0];
    // else ...
    return 0;
}
bool more()
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' )
		return false;
    // else ...
    return true;
}
int takeInValidInt( const string& msg, int min, int max )
{
    int val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
        {
            if( val >= min && val <= max )
                break;
            // else ...
            cout << "\nERROR! Valid ranhe here is " << min
                 << ".." << max << endl;
        }
        else
        {
            cout << "\nERROR!  Only integers valid here ...\n";
            cin.clear(); // clear error flags
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}




Now in main, your logic flow could simply be like this:


	// get some array space to handle vote counts and sorting

	int votes[NUM_CHOICES] = { 0 };
	MyPair myPairs[NUM_CHOICES];
	
	// populate array holding frequency of vote for each person ...
	do
	{
    	showChoices();
        int index = takeInValidInt( "Your choice is : ", 1, NUM_CHOICES );
        ++ votes[ index-1 ];
    }
    while( more() );
    
    // now update array of pairs ...
    for( int i = 0; i < NUM_CHOICES; ++ i )
    {
        myPairs[i].index = i ;
        myPairs[i].count = votes[i] ;
    }
    
// used in debugging ...
/*
    cout << "\nBefore sorting ... \n";
    for( int i = 0; i < NUM_CHOICES; ++ i )
         cout << myPairs[i].index << ", votes = "
              << myPairs[i].count << endl;
*/
    // now sort array of pairs by count in descending order ...
    bsort( myPairs, NUM_CHOICES );

// used in debugging ...
/*
    cout << "\nAfter sorting ... \n";
    for( int i = 0; i < NUM_CHOICES; ++ i )
         cout << myPairs[i].index << ", votes = "
              << myPairs[i].count << endl;
*/
    // now can print top 5  (or all in sorted order ...)
    for( int i = 0; i < NUM_CHOICES; ++ i )
    {
        cout << "votes = " << myPairs[i].count << ", "
             << CHOICES[ myPairs[i].index ] << endl;
    }
    
    cout << "\nPress 'Enter' to continue/exit ... ";
    cin.get();


These all ... really ...
are just some 'baby steps' ...
to hopefully help your student coding days to get started on a good design footing.

This post has been edited by David W: 30 July 2014 - 02:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1