Simple C++ help

A more simple way

Page 1 of 1

10 Replies - 798 Views - Last Post: 17 December 2009 - 07:50 AM Rate Topic: -----

#1 thewhiteberry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 09

Simple C++ help

Post icon  Posted 11 December 2009 - 03:46 PM

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int a,b,c,d,e,f,g,h,i,j,k;
	 

	cout<<"User 1, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>a;

	cout<<"User 2, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>b;

	cout<<"User 3, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>c;

	cout<<"User 4, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>d;

	cout<<"User 5, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>e;

	cout<<"User 6, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>f;

	cout<<"User 7, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>g;

	cout<<"User 8, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>h;

	cout<<"User 9, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>i;

	cout<<"User 10, enter the numnber of pancakes you ate this morning for breakfast!";
	cin>>j;
	int users[10]={a,b,c,d,e,f,g,h,i,j};
		sort(users, users+10);
	if (a==users[9])
		cout<<"Winner for the most pancakes is User 1"<<endl;
	
	if(b==users[9])
			cout<<"Winner for the most pancakes is User 2"<<endl;
	
	if(c==users[9])
			cout<<"Winner for the most pancakes is User 3"<<endl;
	
	if(d==users[9])
			cout<<"Winner for the most pancakes is User 4"<<endl;
	
	if(e==users[9])
			cout<<"Winner for the most pancakes is User 5"<<endl;
		
	if(f==users[9])
			cout<<"Winner for the most pancakes is User 6"<<endl;	
	
	if(g==users[9])
			cout<<"Winner for the most pancakes is User 7"<<endl;

	if(h==users[9])
			cout<<"Winner for the most pancakes is User 8"<<endl;
	
	if(i==users[9])
			cout<<"Winner for the most pancakes is User 9"<<endl;
	
	if(j==users[9])
			cout<<"Winner for the most pancakes is User 10"<<endl;
	
	if (a==users[0])
		cout<<"Winner for the least pancakes is User 1"<<endl;
	
	if(b==users[0])
			cout<<"Winner for the least pancakes is User 2"<<endl;
	
	if(c==users[0])
			cout<<"Winner for the least pancakes is User 3"<<endl;
	
	if(d==users[0])
			cout<<"Winner for the least pancakes is User 4"<<endl;
	
	if(e==users[0])
			cout<<"Winner for the least pancakes is User 5"<<endl;
		
	if(f==users[0])
			cout<<"Winner for the least pancakes is User 6"<<endl;	
	
	if(g==users[0])
			cout<<"Winner for the least pancakes is User 7"<<endl;

	if(h==users[0])
			cout<<"Winner for the least pancakes is User 8"<<endl;
	
	if(i==users[0])
			cout<<"Winner for the least pancakes is User 9"<<endl;
	
	if(j==users[0])
			cout<<"Winner for the least pancakes is User 10"<<endl;
	
	cout<<"thankyou";
cin>>k;

return 0;
}




There has got to be a more simple way of accomplishing this. Thoughts and suggestions welcome.

ignore the cin>>k; at the end it just gives the program a pause before exiting lol

Is This A Good Question/Topic? 0
  • +

Replies To: Simple C++ help

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: Simple C++ help

Posted 11 December 2009 - 03:46 PM

How about a for loop or two?
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,451
  • Joined: 09-June 09

Re: Simple C++ help

Posted 11 December 2009 - 03:56 PM

For starters populate your array like this. Anytime you see an opportunity to shorten code with a loop then its probably a good idea to do so

	int users[10];

	for(int i=0; i<10; i++)
	{
		cout<<"User "<<i+1<<" How many Pancakes did you eat? ";
		cin>>users[i];
	}


Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: Simple C++ help

Posted 11 December 2009 - 03:59 PM

Good job Ima...the lawn needs mowing too.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,451
  • Joined: 09-June 09

Re: Simple C++ help

Posted 11 December 2009 - 04:01 PM

well its not like I showed him the holy grail of programming lol I think after seeing how to use a for loop he will be able to understand it :)

This post has been edited by ImaSexy: 11 December 2009 - 04:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 thewhiteberry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 09

Re: Simple C++ help

Posted 11 December 2009 - 04:57 PM

View PostImaSexy, on 11 Dec, 2009 - 03:01 PM, said:

well its not like I showed him the holy grail of programming lol I think after seeing how to use a for loop he will be able to understand it :)



Actually thank you, it does help alot. I've read on how to do the loops and even went to the tutorial page and still couldn't exactly grasp how to use it. I did this problem just for fun and to learn and now i can see exactly how a for loop is used and why. Makes more sense than just reading someone else doing it. Thank you again.
Was This Post Helpful? 0
  • +
  • -

#7 thewhiteberry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 09

Re: Simple C++ help

Posted 11 December 2009 - 05:05 PM

View PostImaSexy, on 11 Dec, 2009 - 03:01 PM, said:

well its not like I showed him the holy grail of programming lol I think after seeing how to use a for loop he will be able to understand it :)


The last part of the for loop is "cin>>users[i];" i is declared as int and a variable. How can i use this to sort the results?

in my long program I wrote it as ":int users[10]={a,b,c,d,e,f,g,h,i,j};
sort(users, users+10);"
the input from each user was given its own int associated letter that would hold its place.

So i'm just lost on how the individual input from each user is held.
I'm assuming the array holds the input without needing to associate a character.

so if user 1 eats 10 pancakes he is stored in the array users[9];

how can i instead of giving the output as the -winner who ate 10 pancakes- give user 1 is the winner?

i can pull the number from the array but how can i keep that specific number associated with that user??

Sorry just prob really dumb, lol

This post has been edited by thewhiteberry: 11 December 2009 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Simple C++ help

Posted 12 December 2009 - 12:09 PM

User 1 is stored in users[0] (Because computers count starting from zero)

If you're identifying your users by their position in your array, then sorting the array will mean you've lost your user-ID data. You might think about storing their user ID too.

C++ lets you store "pairs" of values. the two values in a pair are called 'first' and 'second' respectively (although when you sort an array-of-pairs, only the first value of the pair is used for comparisons by sort)
#include <map>  // 'pair' exists here
    pair<int, int> users[10];

    for(int i=0; i<10; i++)
    {
        users[i][i][/i].second = i+1;  //UserID
        cout << "How many pancakes did "
             << users[i][i][/i].second << "eat: ";
        cin >> users[i][i][/i].first;  //Pancakes

    } 
"first" is the value to sort by (which, in your case, is the number of pancakes). you could use "second" to store their user ID - then the order of your array won't matter.



incidentally, pair can store any kind of data (as if it were an incomplete sentence, i.e. "pair of..") - the reason for the <int, int> is simply to say "pair of.. int data.. and.. int data".

This post has been edited by Bench: 12 December 2009 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#9 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: Simple C++ help

Posted 12 December 2009 - 11:55 PM

If all you need is the largest value and the smallest value ... you could do something like this:

#include <iostream>
using namespace std;

const int MOUTHS = 10;

int main()
{
    // create an array of integers to hold counts ...
    int user[MOUTHS];
    
    // take in pancakes eaten by each mouth ...
    for(int i = 0; i < MOUTHS; ++i)
    {
        cout << "User '" << char('A'+i)
             << "' breakfast pancakes : ";
        cin >> user[i];
    }
    
    // find max and index ...    
    // begin with max set to actual value of user[0]
    int max = user[0], maxI = 0;
    for(int i = 1; i < MOUTHS; ++i)
    {
        if( user[i] > max )
        {
            max = user[i];
            maxI = i;
        }
    }
    
    cout<<"Winner for the most pancakes is User '"
        << char('A'+maxI) << "' with " << user[maxI] << endl;
            
            
    // find min and index ...
    // begin with min set to actual value of user[0]    
    int min = user[0], minI = 0;
    for(int i = 1; i < MOUTHS; ++i)
    {
        if( user[i] < min )
        {
            min = user[i];
            minI = i;
        }
    }
    
    cout<<"Winner for the least pancakes is User '"
        << char('A'+minI) << "' with " << user[minI] << endl;
    
    cout << "Press 'Enter' to continue ... "<< flush;
    cin.sync();
    cin.get();
}




You might like to see this free tutorial:

BEGINNING COMPUTER PROGRAMMING (using C++ or C)
http://developers-he.../index.p...opic,127.0.html

This post has been edited by David W: 13 December 2009 - 12:00 AM

Was This Post Helpful? 0
  • +
  • -

#10 ThreatToSociety  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-December 09

Re: Simple C++ help

Posted 17 December 2009 - 05:05 AM

instead of using all those if statments you could use the switch command like so :


switch(user){

case: <CONDITION>
//do stuff
case: <CONDITION2>
//do stuff

}



** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Simple C++ help

Posted 17 December 2009 - 07:50 AM

If you want to follow up on ThreatToSociety's suggestion have a read here
http://www.cplusplus...torial/control/
for the full syntax etc of switch statements
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1