#### Archived

This topic is now archived and is closed to further replies.

# Beginner Problem!!

This topic is 6103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Greetings, I am total novice as I only started learning programming at the beginning of January. I would therefore be grateful if anybody could offer me advice on the following: I am trying to write a program that picks six random numbers between 1 & 49, and then displays them in numerical order. This I have managed but am unable to stop two identical numbers coming out. Any ideas? Secondly, I have studied functions and briefly OOP, would these make the code simpler in any way. Finally I have read that goto statements are not very popular. Have I done the right thing by including them here? What would be the alternatives? Thanks in advance. #include <iostream> #include <ctime> #include <cstdlib> #include <vector> using namespace std; char no;// user exits program int main() { vectorNumbers(6); vectorSorted(6); srand(time(0)); short HighestNumber; short HighestIndex; short i, k; cout <<"Welcome to the Lottery Wizard." <>no; if (no == ''n'') { return (0); } else { Begin: for(i=0; i<6; i++) { Numbers = rand() % (49) + 1; } // sort the numbers for (i=0; i<6; i++) { HighestNumber=0; HighestIndex=0; for (k=0; k<6; k++) { if (Numbers[k] > HighestNumber) { HighestNumber = Numbers[k]; HighestIndex = k; } } Sorted[i] = HighestNumber; Numbers[HighestIndex] = 0; } } cout <<"You''re Lucky Numbers are:" <>no; if (no == ''n'') { return (0); } else { goto Begin; } } Here''s my code.

##### Share on other sites
#include <iostream>#include <ctime>#include <cstdlib>#include <vector>using namespace std;char no;// user exits programint main(){	vector<short>Numbers(6);	vector<short>Sorted(6);	srand(time(0));	short HighestNumber;	short HighestIndex;	short i, k;	 	cout <<"Welcome to the Lottery Wizard." <<endl;	cout <<"Would you like me to pick your numbers? y for yes, n for no.\n";	cin >>no;	if (no == ''n'')	{		return (0);	}	else	{         do         {           for(i=0; i<6; i++)	   {		Numbers = rand() % (49) + 1;	   }			   // sort the numbers	   for (i=0; i<6; i++)	   {		HighestNumber=0;		HighestIndex=0;		for (k=0; k<6; k++)		{		     if (Numbers[k] > HighestNumber)		     {			HighestNumber = Numbers[k];			HighestIndex = k;		     }		}		                Sorted[i] = HighestNumber;		Numbers[HighestIndex] = 0;	    }	}	cout <<"You''re Lucky Numbers are:" <<endl;	cout <<endl;	cout <<Sorted[5] <<endl		<<Sorted[4] <<endl		<<Sorted[3] <<endl		<<Sorted[2] <<endl		<<Sorted[1] <<endl		<<Sorted[0];	cout <<endl;		cout << "Would you like another draw? y for yes, n for no.\n";	cin >> no;       } while( no != ''n'' )  return (0);}

##### Share on other sites
Ahhhh!! A do while, thanks anonymous.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 87
• 11
• 10
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009617
• ### Who's Online (See full list)

There are no registered users currently online

×