Write a program with a function main () and selecting a menu of functions:
Generate a programming-random number generator data for lottery ticket / max 100 / with six-digit numbers and store them in an array
-Overwrite generated a new array and sort this array in ascending order and display output
-Counting and display output and numbers of all "happy" six digit lottery tickets /these numbers which sum of the first 3 digits is equal to the last three/
-Save in the array and display output sequence numbers of downloaded "lucky" lottery tickets
I have a problem with last two steps.Can you explain how to do it correctly?
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <iostream>
using namespace std;
int numbers;
int array[100];
void generator()
{
srand((unsigned)time(0));
for(int i = 0; i < 100; i++)
{
numbers = (rand() % 900000) + 100000;
array[i] = numbers;
cout << numbers << endl;
}
}
void sorted_list()
{
int i = 100, a, b, c;
for (a = 0; a < i - 1; a++)
{
for(b = 1; b < i; b++)
{
if (array[b] < array[b - 1])
{
c = array[b];
array[b]= array[b - 1];
array[b - 1]=c;
}
}
}
for (a = 0; a < i; a++)
{
cout << a << ": " << array[a] << "\n";
}
}
bool happyNr(int a){
if (a<100000 || a>999999) return false;
int half1=0, half2=0;
for (int i=0;i<6;i++){
if (i<3) {half1+=a%10; a=a/10;}
else {half2+=a%10; a=a/10;}
}
return half1==half2;
}
int menu()
{
int choice;
cout<<"\n_______________MENU_______________";
cout<<"\n 1. Generate random numbers";
cout<<"\n 2. Sorted array";
cout<<"\n 3. Happy numbers";
cout<<"\n 4. Exit";
do
{
cout<<"\n Choice: ";
cin>>choice;
}while(choice<1||choice>4);
return(choice);
}
int main()
{
int i;
do
{
i=menu();
switch(i)
{
case 1: generator();break;
case 2: sorted_list();break;
case 3: happyNr(i);break;
}
}
while(i!=4);
return 0;
}
While doing my course project of card game, i got this error
Invalid types `int[int]' for array subscript'
Here is the code :
#include <iostream>
#include <stdlib.h>
#include <time.h>
int randomize(int, int);
int* generateCardNumber(int[5]);
int* generateCardLogo(int, int[13][4], int[5]);
int* compareCard(int[5], int[5], int[5], int[5], int[13][4]);
int printResult(int[5]);
void showCard(int, int[5], int[5], int[5], int[5]);
void printCard(int[5], int[5]);
using namespace std;
int main()
{
srand(time(NULL));
int player1Number[5], player1Logo[5];
int *number1, *logo1;
int player2Number[5], player2Logo[5];
int *number2, *logo2;
int cardInfo[13][4];
int **card;
char answer;
do
{
int playerInput;
int d, c, r;
int player1Score, player2Score;
cout<<"Welcome to the card game!"<<endl;
cout<<" Choose player{1: Player 1; 2: Player 2} : ";
cin>>playerInput;
cout<<endl;
do
{
cout<<"Press d to deal card :";
}while(d != 'd');
number1 = generateCardNumber(player1Number);
logo1 = generateCardLogo(1,cardInfo, player1Number);
for (int i = 0; i<5; i++)
{
cardInfo[player1Number[i]][player1Logo[i]] = 1;
}
number2 = generateCardNumber(player2Number);
logo2 = generateCardLogo(2,cardInfo, player2Number);
for (int i = 0; i<5; i++)
{
cardInfo[player2Number[i]][player2Logo[i]] = 1;
}
showCard(playerInput,player1Number,player1Logo,
player2Number,player2Logo);
do
{
cout<<"Press c to compare card :";
}while(c != 'c');
*card = compareCard(player1Number,player1Logo,
player2Number,player2Logo,cardInfo);
for (int i = 0; i<5; i++)
{
if(cardInfo[player1Number[i]][player1Logo[i]] = -1)
{
player1Number[i] = -1;
player1Logo[i] = -1;
}
else if(cardInfo[player2Number[i]][player2Logo[i]] = -1)
{
player2Number[i] = -1;
player2Logo[i] = -1;
}
}
showCard(playerInput,player1Number,
player1Logo,player2Number,player2Logo);
do
{
cout<<"Press r to show result :";
}while(r != 'r');
if(playerInput == 1)
{
cout<<"Player1 (You): ";
player1Score = printResult(player1Number);
cout<<"Player2 (Computer): ";
player2Score = printResult(player2Number);
if(player1Score > player2Score) cout<<"You WIN!"<<endl;
else cout<<"Computer WIN!"<<endl;
}
else if(playerInput == 2)
{
cout<<"Player1 (Computer): ";
player1Score = printResult(player1Number);
cout<<"Player2 (You): ";
player2Score = printResult(player2Number);
if(player2Score > player1Score) cout<<"You WIN!"<<endl;
else cout<<"Computer WIN!"<<endl;
}
cout<<"Do you want to play again? (y/n)"<<endl;
cin>>answer;
}while(answer == 'Y' || answer == 'y');
}
int randomize (int x, int y)
{
return (rand()%y + x);
}
int* generateCardNumber (int numberArray[5])
{
int arrayStoring[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
for (int i = 0; i<5; i++)
{
do
{
numberArray[i] = randomize(1,13);
}while (arrayStoring[numberArray[i]] == 1);
arrayStoring[numberArray[i]] = 1;
}
return numberArray;
}
int* generateCardLogo (int turn, int cardInfo[4][13], int player2Number[5])
{
int logoArray[5];
if(turn == 1)
{
for (int i = 0; i<5; i++)
{
logoArray[i] = randomize(1,4);
}
return logoArray;
}
else if(turn == 2)
{
for (int i = 0; i<5; i++)
{
do
{
logoArray[i] = randomize(1,4);
}while (cardInfo[player2Number[i]][logoArray[i]] == 1);
}
return logoArray;
}
}
int** compareCard(int player1Number, int player1Logo, int player2Number, int player2Logo, int cardInfo)
{
for(int i=0; i<5 ; i++)
{
for(int j=0; j<5 ; j++)
{
if(player1Number[i] == player2Number[j])
{
if(player1Logo[i] < player2Logo[j]) cardInfo[player1Number[i]][player1Logo[i]] = -1;
else if(player1Logo[i] > player2Logo[j]) cardInfo[player2Number[i]][player2Logo[i]] = -1;
break;
}
}
}
return cardInfo;
}
int printResult (int playerNumber)
{
int playerScore = 0;
for (int i = 0; i<5; i++)
{
if(playerNumber[i] == -1) break;
else if(playerNumber[i] == 0)
{
playerScore += 15;
cout<<"15 ";
}
else if(playerNumber[i] > 0 && playerNumber[i] < 10 )
{
playerScore += (playerNumber[i] + 1);
cout<< (playerNumber[i] + 1) <<" ";
}
else if(playerNumber[i] >= 10)
{
playerScore += 12;
cout<<"12 ";
}
if(i<4) cout<<"+ ";
else if(i==4)
{
cout<<"= "<<playerScore<<" points";
}
}
return playerScore;
}
void printCard (int numberArray[5], int logoArray[5])
{
for (int i = 0; i<5; i++)
{
switch(numberArray[i])
{
case -1 :
cout<<"<fold> ";
break;
case 0 :
cout<<"Ace ";
break;
case 1 :
cout<<"2 ";
break;
case 2 :
cout<<"3 ";
break;
case 3 :
cout<<"4 ";
break;
case 4 :
cout<<"5 ";
break;
case 5 :
cout<<"6 ";
break;
case 6 :
cout<<"7 ";
break;
case 7 :
cout<<"8 ";
break;
case 8 :
cout<<"9 ";
break;
case 9 :
cout<<"10 ";
break;
case 10 :
cout<<"Jack ";
break;
case 11 :
cout<<"Queen ";
break;
case 12 :
cout<<"King ";
break;
}
switch(logoArray[i])
{
case -1:
break;
case 0:
cout<<"Diamond ";
break;
case 1:
cout<<"Club ";
break;
case 2:
cout<<"Heart ";
break;
case 3:
cout<<"Spade ";
break;
}
}
}
void showCard (int playerInput, int player1Number, int player1Logo, int player2Number, int player2Logo)
{
if(playerInput == 1)
{
cout<<"Player1 (You):"<<endl;
printCard(player1Number, player1Logo);
cout<<"Player2 (Computer):"<<endl;
printCard(player2Number, player2Logo);
}
else if(playerInput == 2)
{
cout<<"Player1 (Computer):"<<endl;
printCard(player1Number, player1Logo);
cout<<"Player2 (You):"<<endl;
printCard(player2Number, player2Logo);
}
}
Honestly, i still newbie at programming. So i didn't know that i can't return a whole array from a function. After searching for clue, i found out that i can use pointer to return an array from function. When i try to fix the code, this error come out and i didn't know what i need to fix.
The error keep appearing when i try to use the array from the function parameter,such as this
if(player1Number[i] == player2Number[j])<br/>
from function
int** compareCard(int player1Number, int player1Logo, int player2Number,
int player2Logo, int cardInfo)
and onward.
Can someone help me figure this out? Correction to the code would be very appreciated. Thankyou very much! (Sorry if my english is bad)
You're declaring compareCard with a bunch of arrays (so int pointers) as parameters:
int* compareCard(int[5], int[5], int[5], int[5], int[13][4]);
Then, however, you're implementing it using integers (not pointers) as parameters:
int** compareCard(int player1Number, int player1Logo, int player2Number, int player2Logo, int cardInfo)
{
for(int i=0; i<5 ; i++)
You're also using them as arrays:
if(player1Logo[i] < player2Logo[j]) cardInfo[player1Number[i]][player1Logo[i]] = -1;
Also, the return type is wrong. You're declaring it as int*, but then implement it as int**. You have to ensure that the signature of your function matches. It looks like taking the signature of the declaration and applying it to the definition should fix this compile time error.
Does your compiler issue any warnings for these function declarations? If not, see if you can set it to be more strict with warnings. Getting the correct warnings for potential errors greatly increases debug efficiency.
my .exe well compiled program stops working after run it in CodeBlocks,
It stops after typing cin>>f.name the to the console after the choosed choice
I have a Windows 8 and i use GNU GCC compiler here is the code,maybe there is a code error , i don t know
tanks for your attention
:
using namespace std;
struct employee
{
string name;
int age;
};
employee employeeList[10];
class Stack
{
int pos;
public:
Stack(){};
void push(employee e)
{
employeeList[pos] = e;
pos++;
}
employee pop(int n)
{
if(n = pos - 1)return employeeList[pos];
if(n < pos - 1)
{
return employeeList[pos];
for(int j =n; j < pos; j++ )
{
employeeList[pos] = employeeList[pos + 1];
}
}
pos--;
}
string print(int n)
{
n = pos;
cout<<employeeList[pos].name<<endl;
}
char menu()
{
char choice;
cout << "Press 1. to push an employee"<<endl;
cout << "Press 2. to pop an employee"<<endl;
cout << "Press 3. to show an employee"<<endl;
cin>> choice;
return choice;
}
};
int main()
{
Stack s;
char input = s.menu();
int j;
do
{
switch(input)
{
case '1' :{employee f; cin>>f.name; s.push(f);}break;
case '2' :{int n; cin>>n; s.pop(n);} break;
case '3' :{int n; cin>>n; s.print(n);}break;
}
j++;
}
while(j < 10);
return 0;
}
You didn't proper initialization of instance variable "pos" of class "Stack". This means that initially it can have any value. If that value is beyond the range of your "employeeList", you are accessing an area of memory that does not belong to you. That's probably a segmentation error.
I have the following code:
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
using namespace std;
class sekuence
{
public:
sekuence() {
string emra[5][20];
int m=0;
}
void shto (){
int i;
int j;
char temp[20];
cout << "=============================="<<endl
<< "Shkruani emrat qe doni te shtoni ne vektor"<<endl;
for(i=0; i < 5; i++){
cin >> emra[i];
m++;
}
for(i=0; i<5; i++)
{
for( j=1; j<5; j++){
if(strcmp(emra[j-1],emra[j]) > 0) {
strcpy(temp,emra[j-1]);
strcpy(emra[j-1],emra[j]);
strcpy(emra[j],temp);
}
}
}
}
void print(){
for(int i=0; i<5 ; i++){
cout<<endl<<emra[i];
}
cout <<m;
}
bool kerko (const char *gjej)
{
for(int i=0; i<5; i++)
{
if(strcmp(emra[i],gjej) == 0 )
return true;
return false;
}
}
int n (){return m;}
private:
int m;
char emra[5][20];
};
int main()
{ char* gjej;
int x;
sekuence d;
do{
cout<<endl << " =========================" << endl;
cout<<"1 - Funksioni i shtimit" <<endl;
cout<<"2 - Funksioni i afishimit" <<endl;
cout<<"3 - Funksioni i kerkimit" <<endl;
cout<<"4 - Funksioni i fshirjes" <<endl;
cout<<"5 - Dalje nga programi" <<endl<<endl;
cout<<"====================================" <<endl;
cin >> x;
switch (x)
{
case 1:
d.shto();
break;
case 2:
d.print();
break;
case 3:
cout<<"==================================" <<endl
<< "Shkruani emrin qe doni te kerkoni"<<endl;
cin >> gjej;
d.kerko(gjej);
break;
case 4:
break;
case 5:
cout << "Dalje nga Programi" <<endl;
exit (0);
default:
cout <<"Zgjedhje e gabuar";
return 0;
}}
while (x != 5);
return 0;
}
Everytime I try to run the bool function I get an error..i don't what to do.
when i try to enter the nam i want to fine the function bool kerko() and quit the prog. and want find the char i want to find it opens the visual studio debugger.
You are trying to access gjej (char*) but no memory allocated to it. Try allocating some memory using new
char* gjej = NULL;
gjej = new char[NumberOfChars];
Edit: used new instead of malloc now..
std::string would be even nicer..
According to your indentation of the kerko function, one of the return statements should be moved out of the for loop:
bool kerko (const char *gjej)
{
for(int i=0; i<5; i++)
{
if(strcmp(emra[i],gjej) == 0 )
return true;
}
//*** Note the new location.
return false;
}
The layout you have makes the bool function always return before the 2nd iteration of the loop.
Ok so I've already simplified/condense it while keeping the functionality,but im only doing C++ for a month and a half.Was at 100 lines of code.Is it possible to declare the variable in the functions arguement and then call them without passing values into the arguemnets?
#include <iostream>
#include <windows.h>
using namespace std;
int primeCheck10 (int j)
{
int count=0;
cout<<"Enter a number between 1 and 10___";
cin>>j;
if(j<1 ||j>10)
{
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<j; i++)
{
if(j%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
}
int primeCheck100(int j)
{
int count=0;
cout<<"Enter a number between 1 and 100___";
cin>>j;
if(j<1 || j>100){
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<j; i++)
{
if(j%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
}
int checkPrime1000(int j)
{
int count=0;
cout<<"Enter a number between 1 and 1000___";
cin>>j;
if(j<1 || j>1000){
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<j; i++)
{
if(j%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
}
int main ()
{
system("pause");
return 0;
}
Yes, you can condense all the prime checking into a single function pretty easily. I'd change the structure of the code to just check whether a given number is prime, and return a bool to indicate whether it is or not:'
bool isprime(int n) {
int limit = sqrt(n)+1; // only need to check up to sqrt(n)
if (n == 2)
return true;
if (n == 1 || n % 2 == 0) // check if it's 1 or even
return false;
for (int i = 3; i <= limit; i += 2) // not even -- only check odd numbers
if (n % i == 0)
return false;
return true;
}
Then the code to get input and display results would be separate:
void primecheck(int limit) {
std::cout << "Please enter a number between 1 and " << limit;
int j;
std::cin >> j;
if (j<1 || j > limit)
std::cerr << "Invalid value";
static char const *labels [] = { "Not a prime number\n", "Prime number\n" };
std::cout << labels[isprime(j)];
}
It wouldn't be terribly difficult to make it even shorter than this, but we're reaching the point where it would probably end up less readable if you did so.
If I have unserstood correctly you then you are asking about default arguments. For example
#include <iostream>
void f( int i = 10 )
{
std::cout << "i = " << i << std::endl;
}
int main()
{
f();
}
You could just make one method... and add a bounds
int primecheck(int value, int bounds){
int count=0;
cout <<"Enter a number between 1 and " << bounds << "___";
cin>>value;
if(j<1 || value>bounds){
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<value/2; i++)
{
if(value%i==0)
{
count++;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
return count;
}
This alg is prob not the best way to check for primes though. for instance for(int i=2; i<j/2; i++) in your loop with optimize it. For example 32, 6x6 = 32, 2x16 = 32, 4x8 = 32. The highest number that can be divisible to j or value is half of it because 1 isn't considered under divisibility so the lowest number would be 2. Which is half.