i am making a simple program that displays the behaviour of the person from the sentences that i write in switch statement cases. I want to randomly generate a number between 1 and 10 and use it in "switch(this place)". So far, i have written the following code and stuck here..
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int rand_range(int low, int high)
{
return rand()%(high-low) + low;
}
int main()
{
srand(time(NULL));
cout<<"Hi!, this program tells you about your behaviour"<<endl;
cout<<"press enter to continue";
cin.get();
switch( )
{
case 1:
{
cout<<"you are rude !";
}
case 2:
{
cout<<"you are smart";
}
case 3:
{
cout<<"you try to prove yourself special all the time";
}
case 4:
{
cout<<"you are good in sarcasm";
}
case 5:
{
cout<<"you look dumb, but deep inside you are intelligent";
}
case 6:
{
cout<<"you are always ready for a debate with someone";
}
case 7:
{
cout<<"you are very lazy";
}
case 8:
{
cout<<"you are dumber than what you look";
}
case 9:
{
cout<<"you always try to help others";
}
case 10:
{
cout<<"you have good decision making capabilities";
}
default:
{
cout<<"something wrong";
}
}
}
In your switch statement in the parentheses you want (rand() % 10) + 1. After you have seeded the random number generator the rand() function will return a number between 0 and RAND_MAX. RAND_MAX is a large number.
To get a random number in a range a standard trick is to use the % operator. rand() % 10 returns a number between 0 and 9 inclusive. Add 1 to get your range of 1 through 10.
Related
I was solving the a question at HackerRank -
Question Link - https://www.hackerrank.com/challenges/maximum-element/problem
In one solution I used 'break' statement in switch-case, in another solution I didn't.
Solution was wrong when I didn't use break statement. What is the reason behind this ?
Input -
10
1 97
2
1 20
2
1 26
1 20
2
3
1 91
3
With break Statement -
#include <bits/stdc++.h>
using namespace std;
int main() {
int noOfTestCases;
cin>>noOfTestCases;
vector <int> st;
for(int x=0; x<noOfTestCases; x++){
int query;
cin>>query;
switch (query) {
case 1:
int number;
cin>>number;
if(st.empty()){
st.push_back(number);
}
else if(number > st[st.size()-1]){
st.push_back(number);
}
else{
st.push_back(st[st.size()-1]);
}
break;
case 2:
if(!st.empty()){
st.pop_back();
}
break;
case 3:
cout<<st[st.size()-1]<<endl;
}
}
}
//Output -
//26
//91
Without break statement -
#include <bits/stdc++.h>
using namespace std;
int main() {
int noOfTestCases;
cin>>noOfTestCases;
vector <int> st;
for(int x=0; x<noOfTestCases; x++){
int query;
cin>>query;
switch (query) {
case 1:
int number;
cin>>number;
if(st.empty()){
st.push_back(number);
}
else if(number > st[st.size()-1]){
st.push_back(number);
}
else{
st.push_back(st[st.size()-1]);
}
case 2:
if(!st.empty()){
st.pop_back();
}
case 3:
cout<<st[st.size()-1]<<" "<<query<<endl;
}
}
}
//Output -
//0
//0
//0
//0
//0
//0
//0
//0
//0
//0
Consider the following code
switch (x) {
case 1:
std::cout << "one\n";
case 2:
std::cout << "two\n";
break;
case 3:
std::cout << "three\n";
}
If x is 1 it will print both one and two. It will then exit the switch block due to the break statement. Note that x will not be compared with 2 after printing "one", it will directly fall-through to printing "two".
So I had a misconception about switch case statement -
What I thought -
I thought that in switch case if the expression is equal to any case D, then further cases - E, F, G, will not be executed, irrespective of the break statement.
Reality - If an expression is equal to any case D, then further cases - E, F, G will also be executed if we don't use break statement.
Thanks Jeffrey and Paul Sanders to clarify this problem.
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if( 1<=n<=9)
{
switch(n)
{
case 1:cout<<"one"; break;
case 2:cout<<"two"; break;
case 3:cout<<"three"; break;
case 4:cout<<"four" ;break;
case 5:cout<<"five" ;break;
case 6:cout<<"six" ;break;
case 7:cout<<"seven"; break;
case 8:cout<<"eight" ;break;
case 9:cout <<"nine";
}
}
else
{ cout<<"greater than nine"; }
}
the above code when i am running (or)compiling my else statement is not working
for example if i am giving a number like 44 it is not displaying the statement in else that it is grater than nine but the if case is working nicely.
You have a problem with your if condition.
It should be like this:
if (n >= 1 && n <= 9)
As it is now, it means
if ((1 <= n) <= 9)
Which will always evaluate to true.
Try changing the condition to
if (1<=n && n<=9)
So when I put the limits of the integer that the switch case uses I keep getting a infinite number of the output of default for example:
int (num);
cout<<"Choose a number between 1-5"<<endl;
cin>>num;
while (num<1 || num>5)
{
switch (num)
{
case 1:
cout<<" Good"<<endl;
break;
case 2 :
cout<<" Okay"<<endl;
break;
case 3:
cout<<" Decent"<<endl;
break;
case 4:
cout<<" Nice Try"<<endl;
break;
case 5:
cout<<"Failed"<<endl;
break;
default
cout<<" Not Valid"<<endl;
break;
{
{
So for this example how would I make the user choose from 1-5 and if not repeat the loop in order for them to try again.
This will loop until valid number is given then test it in the switch case.
int num;
cout<<"Choose a number between 1-5"<<endl;
while(cin>>num && (num < 1 || num > 5));
switch (num)
{
... //for brevity
}
This will loop through the switch cases until a valid input is given.
int num;
bool keepLooping = true;
cout<<"Choose a number between 1-5"<<endl;
while(cin>>num && keepLooping)
{
keepLooping = false;
switch(num)
{
... //for brevity
default:
keepLooping = true;
}
}
So i need to have my switch statement go through and write out five two if the user says 52 but i cannot get it pass my 0-9. if they type 0-9 it works perfect but if i try to do any number past that it makes a blank. help!
#include <stdio.h>
int main (void)
{
int x;
printf("Please enter an integer: ");
scanf("%d", &x);
printf("\nYou have entered:\n\n");
for(x;x<0;x++);
switch (x)
{
case 0:
printf("zero");
break;
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
case 5:
printf("five");
break;
case 6:
printf("six");
break;
case 7:
printf("seven");
break;
case 8:
printf("eight");
break;
case 9:
printf("nine");
break;
}
printf("\n\n");
return 0;
}
do {
switch (x%10) {
...
}
x = x / 10;
} while (x>0) ;
or to get it in the right order use recursion
void f(int x) {
if (x==0) return;
f(x/10);
switch(x%10) { ... }
}
This question has been asked-and-answered before.
The for-loop you wrote is empty, because the body of the loop ends with the semi-colon:
for(x;x<0;x++) /* Empty Body!!*/ ;
The way a typical for loop works is:
for( /*Initialize*/; /*Test*/; /*Change*/)
{
/* Body */
}
In your case, I think you want:
for(int i=0; i < x; ++i)
{
switch(x)
{
[...]
}
}
This will:
Initialize i to 0
Test if i is LESS THAN x (the number you entered)
Keep increasing i by 1, until it gets up to x.
I'm not going to do your homework for you but consider the following pseudo-code:
print_text_digits(x)
{
if (x >= 10) print_text_digits(x / 10);
switch (x) {
print "zero" through "nine" as appropriate
}
}
main()
{
scan number into x;
print_text_digits(x);
}
This relies on a recursive routine so that you get your digits processed one at a time, with the might significant digit printed first.
You could solve this with recursion.
void printDigit(int x) {
int digit = x%10;
if(digit!=x)
printDigit(x/10);
switch(digit) {
...
}
}
This will print the most significant figure first, unlike the while loops most people are mentioning.
I believe you need this:
#include <stdio.h>
int main (void)
{
int count = 0;
int x, count2;
printf("Please enter an integer: ");
scanf("%d", &x);
printf("\nYou have entered:\n\n");
int aux = x;
while(aux>0) {
aux=aux/10;
count++;
}
count2 = count;
while(count) {
aux = x;
for(int i=count-1;i>0;i--)
aux=aux/10;
for(int i=count2-count;i>0;i--)
aux%=10;
switch (aux) {
case 0:
printf("zero");
break;
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
case 5:
printf("five");
break;
case 6:
printf("six");
break;
case 7:
printf("seven");
break;
case 8:
printf("eight");
break;
case 9:
printf("nine");
break;
}
count--;
if(count) printf(" ");
}
printf("\n\n");
return 0;
}
Now, with an input 52 it will propelly return five two.
#include <stdio.h>
static const char * const num[] = {
"zero ", "one ", "two " , "three ", "four ",
"five ", "six ", "seven ", "eight ", "nine "
};
void printNum(int x)
{
if (x < 10) {
printf(num[x]);
return;
}
printNum(x / 10);
printNum(x % 10);
}
int main (void)
{
int x;
printf("Please enter an integer: ");
scanf("%d", &x);
printf("\nYou have entered:\n\n");
printNum(x);
return 0;
}
Here's what you need to do. Similar to what #simonc said, it's a good idea to convert the user input to a string, then loop through the characters. For each character, convert it into an int and then take that into the switch statement.
EDIT---------------------------
Here's a way with strictly using integers.
First find how many digits your integer has. You can do this by a method mentioned here.
Then do integer division starting from the largest power of 10 that divides the integer you have and divide the divisor by 10 until you reach 1. For example:
If user input is 213, it has 3 digits. We divide this by 100 first.
213/100 = 2
We take the 2 and put it into the switch statement, outputting 2. Now we're done with the hundreds digit, so now we take the 13 from 213 and divide it by 10.
13/10 = 1 So now we output one.
Keep doing this process until you get to the ones digit.
Does this make sense?
The variable "probability" always comes out to zero even though the outputs of the other variables are in the same placement area as the probability variable at the end of the program. I have a feeling it has to do with placement, but it could be another initializing problem. The probability should never come out to zero.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main() {
int die1,
die2,
sum,
turns,
win=0,
loss=0;
double probability=0;
int thepoint, rolls;
srand(time(0));
cout<<"How many turns would you like? ";
cin>>turns;
for(int i=0; i<turns; i++)
{
sum=0;
die1=rand()%6;
die2=rand()%6;
sum=die1+die2;
//cout<<"\nFirst die is a "<<die1<<endl;
//cout<<"Second die is a "<<die2<<endl;
//cout<<"\n\n>>>Turn "<<i<<": You rolled "<<sum;
switch (sum){
case 2: case 3: case 12:
//cout<<"You have lost this turn with 2 3 or 12!"<<endl;
loss++;
break;
case 7:
//cout<<"\nYea! You won this turn with a 7 on the first roll!"<<endl;
win++;
break;
case 11:
//cout<<"You won this turn ith 11!"<<endl;
win++;
break;
default:
//cout<<"\nRolling again!"<<endl;
thepoint=sum;
rolls=1;
sum=0;
//cout<<"\nRoll 1 - Your point is "<<thepoint<<endl;
while (sum != thepoint)
{
//srand(time(0));
die1=rand()%6;
die2=rand()%6;
sum=die1+die2;
rolls++;
//cout<<"Roll "<<rolls<<". You rolled "<<sum<<endl;
if (sum == thepoint)
{
//cout<<"You won this turn in the while with a point match!"<<endl;
win++;
break;
}
if (sum == 7)
{
loss++;
//cout<<"You lost this turn in the while with a 7"<<endl;
break;
}
}
}
}
probability = win/turns;
cout<<"No. of Turns: "<<turns<<"\n";
cout<<"No. of Wins: "<<win<<"\n";
cout<<"No. of Loses: "<<loss;
cout.precision(6);
cout<<"\nExperimental probability of winning: "<<fixed<<probability;
cin.get();
cin.get();
return 0;
}
Since the variables win and turns are of datatype int, and probability is a real number (i.e. double here) you would need to cast at least one of them to double before you perform the division.
probability = (double)win/turns;
BTW, there is no harm in casting both win and turns too if required but not really needed.
The mod 6 operation % 6 will give you a number in the range 0..5 (the remainder after dividing by 6). You need to add 1 to that.
change
die1=rand()%6;
die2=rand()%6;
to
die1=1+rand()%6;
die2=1+rand()%6;
Update: Although this is a bug, it is not the root cause of the probability printing zero, the real problem was pointed out by #Tuxdude.