Hello I'm new to C++ and was hoping to find some advice/tutoring with an issue I'm having. This is a homework code however I'm really just looking for help to understand what I'm doing wrong. I was doing well until we got to functions and I seem to have a hard time with it. Any assistance would be greatly appreciated.
I receive the following 2 errors when debugging:
warning C4244: 'initializing' : conversion from 'time_t' to 'unsigned int', possible loss of data
error C2082: redefinition of formal parameter 'flip'
Description: Program simulates a coin toss. When you call the function , it should generate a random number in the range of 1 through 2.
If the random number is 1 the display should say “heads”. If the random number is 2 the display should say “tails”.
Program will ask the user to select how many times the coin should be tossed. Then loop the correct number of times and display the results.
Limitations or issues:
Credits: Not Applicable
*/
#include <iostream>
#include <cstdlib> //for rand and srand
#include <ctime> //for time function
usingnamespace std;
int coinFlip(int); //function prototype
int main()
{
int tosses, count; //Variables defined
count = 0;
cout << "How many tosses should I make?" << endl; //ask for user input
cin >> tosses;
while (count <= tosses) //run loop while less than tosses inputed
{
cout << coinFlip << endl;
count++;
}
system("pause");
return 0; //end code
}
int coinFlip(int flip) //flip function
{
unsigned seed = time(0); //get system time
srand (seed); //Seed the random number generator
int flip = 1 + rand() % 2; //calculates either 1 or 2
if (flip == 1)
{
cout << "Heads!.\n"; // display heads if 1
}
elseif (flip == 2)
{
cout << "Tails!.\n"; //display tails if 2
}
return flip;
}
The error is because you're passing a variable called flip into your coinFlip function, and then trying to declare another variable with the same name inside of the function. The warning is just because time_t numbers can go below zero but an unsigned int cannot.
Okay I changed the function name and no longer receive any debugging warnings or errors. However I must be missing something within the code that I just can't see. When the code is ran I receive the following:
How many tosses should I make?
6
01171023
01171023
01171023
01171023
01171023
01171023
01171023
Press any key to continue . . .
instead of: eg.
How many tosses should I make?
3
Heads!
Tails!
Tails!
Here's the code now:
Description: Program simulates a coin toss. When you call the function , it should generate a random number in the range of 1 through 2.
If the random number is 1 the display should say “heads”. If the random number is 2 the display should say “tails”.
Program will ask the user to select how many times the coin should be tossed. Then loop the correct number of times and display the results.
Limitations or issues:
Credits: Not Applicable
*/
#include <iostream>
#include <cstdlib> //for rand and srand
#include <ctime> //for time function
usingnamespace std;
int coinFlip(int flipFunc); //function prototype
int main()
{
int tosses, count; //Variables defined
count = 0;
cout << "How many tosses should I make?" << endl; //ask for user input
cin >> tosses;
while (count <= tosses) //run loop while less than tosses inputed
{
cout << coinFlip << endl;
count++;
}
system("pause");
return 0; //end code
}
int coinFlip(int flipFunc) //flip function
{
unsigned seed = time(0); //get system time
srand (seed); //Seed the random number generator
int flip = 1 + rand() % 2; //calculates either 1 or 2
if (flip == 1)
{
cout << "Heads!.\n"; // display heads if 1
}
elseif (flip == 2)
{
cout << "Tails!.\n"; //display tails if 2
}
return flip;
}
At this point I've missed the deadline for passing it in I'm just getting bothered that I can't figure this out. Any suggestions would be great Thanks
cout << coinFlip << endl;
That's not how you call a function. You need the parenthesis and the int parameter (which is unused and should be removed :P) cout << coinFlip(1337) << endl;
#include <iostream>
#include <cstdlib> //for rand and srand
#include <ctime> //for time function
usingnamespace std;
int coinFlip(); //function prototype
constint HEADS = 1;
constint TAILS = 2;
int main()
{
int tosses, count; //Variables defined
count = 0;
cout << "How many tosses should I make?" << endl; //ask for user input
cin >> tosses;
while (count <= tosses) //run loop while less than tosses inputed
{ coinFlip();
count++;
}
system("pause");
return 0; //end code
}
int coinFlip() //flip function
{
srand((unsigned)time(0)); //Seed the random number generator
int flip = (rand()%2)+1; //calculates either 1 or 2
//cout << flip << endl; //testing
if (flip == HEADS)
cout << "Heads!.\n"; // display heads if 1
elseif (flip == TAILS)
cout << "Tails!.\n"; //display tails if 2
return flip;
}//end flip function
eg.
How many tosses should I make?
7
Heads!.
Heads!.
Heads!.
Heads!.
Heads!.
Heads!.
Heads!.
Heads!.
Press any key to continue . . .