although default part mustn't run,
it runs al case, I thanks for all helps at now, what should i do? This code all runs i writes on screen default case.
do
{
scanf("%c",&choice);
switch(choice)
{
case 'T':
printf("Enter edges of Triangle1\n");
scanf("%d%d",&edge1,&edge2);
Triangle1(edge1,edge2);
break;
case 't':
printf("Enter edges of Triangle1\n");
scanf("%d%d",&edge1,&edge2);
Triangle2(edge1,edge2);
break;
case 'R':
printf("Enter edges of square\n");
scanf("%d%d",&edge1,&edge2);
Rectangle(edge1,edge2);
break;
case 'S':
printf("Enter one edge of square\n");
scanf("%d",&edge);
Square(edge);
break;
case 'C':
printf("Enter radius of circile\n");
scanf("%d",&radius);
Circle(radius);
break;
default:
printf("Wrong input\n");
break;
}//end of switch
printf("\n");
}while(choice!='e');
The newline gets passed as well as input. Try calling "getc(stdin)" after the first scanf() and it should work :)
Check out for example: http://home.datacomm.ch/t_wolf/tw/c/getting_input.html#newline
Related
I’m new to C and currently doing a Jack ‘N Poy that requires the project to restart when the user prompt it like by answering y. However, I can’t seem to grasp its pattern. Have tried it on C++ and it worked but not on C. Can anyone help? I want the user to have the ability to play again after finishing the game.
Here is the code:
#include<stdio.h>
int main (){
char firstPlayer, secondPlayer, again;
do{
printf("Jack 'n Poy)\nEnter Player 1 input: ");
scanf("%c ", &firstPlayer);
printf("Enter Player 2 input: ");
scanf("%c", &secondPlayer);
switch (firstPlayer){
case 'x':
switch (secondPlayer){
case 'x':
printf("Draw");
break;
case 's':
printf("Player 2 wins");
break;
case 'p':
printf("Player 1 wins");
}
break;
case 's':
switch (secondPlayer){
case 'x':
printf("Player 1 wins");
break;
case 's':
printf("Draw");
break;
case 'p':
printf("Player 2 wins");
}
break;
case 'p':
switch (secondPlayer){
case 'x':
printf("Player 2 wins");
break;
case 's':
printf("Player 1 wins");
break;
case 'p':
printf("Draw");
}
break;
default:
printf("Invalid input");
}
printf("\nPlay again? (y/n)");
scanf("%c", &again);
}
while (again == 'y');
printf("Thank you for playing");
return 0;
}
Have also tried adding a substitute variable for the y and yes it does loop, but the loop skips the first question.
Your help are very much appreciated. Thank you very much!
You need to change the format to:
scanf(" %c", ...);
in all scanfs. Note the ' ' before %c
Your again is a string. You can use strcmp(expectedString,inputString) as the condition at while. You will need to include the string.h header file to be able to do this.
Alternatively, change again to char type, i.e. char again;
And then use %c in the last scanf, i.e. scanf("%c", &again);. You may face skipped character scenario if spaces or newlines are entered. In that case, refer to this answer for guidance.
while(Game == 0)
{
gotoxy(100,40);
setForeColor(MY_COLOR_WHITE);
printf("A- Jogar uma partida de Anabi\n");
gotoxy(100,42);
printf("B- Carregar partida anterior\n");
gotoxy(100,44);
printf("C- Descri%c%co do jogo\n", 135, 198);
gotoxy(100,46);
printf("D- Sair do jogo\n");
scanf("%c", &choice);
switch(choice)
{
//This part of the code should break my loop
case 'A':
case 'a':
gotoxy(100, 52);
printf("Please enter your name bellow\n");
gotoxy(98, 53);
printf("->");
scanf("%s",NamePlayer);
Game = 1;
break;
case 'B':
case 'b':
// in here I am going to call a function, the function that reads the file previously saved
//also here I am going to know if there is any saved files, and give an output to the user
break;
case 'C':
case 'c':
//This gets me to the description of the game, rules, etc.. and its working fine with the loop
DescricaoDoJogo();
break;
case 'D':
case 'd':
exit(0);
break;
default:
//This default is giving me trouble, should I use and If, else? Because when i choose the option C, and I get back to the menu what is inside the default executes
printf("Wrong choice.. Try again.. A, B, C or D..");
scanf(" %c", &choice);
system("cls");
break;
}
}
I am a newbie to Computer Science and I'm currently studying first year in university.
I have a problem when comes to one question today.
The question is this :
Write a program to allow the user to enter code and quantity sold for 3 products (use different variables to store these 5 different product codes). Your program is required to calculate the total amount for each product (quantity x unit price) and the grand total of all products sold.
At the end, your program will display product code, product description, unit price and total amount for each product and also the grand total amount for all the products sold. Use the information on the following table as reference:
PS: you will be expected to have many variables used and many redundant of same codes appear in your program which is fine, because no looping is needed to apply here.
Extra requirements :
Set the price to 2 decimal points
Fix the product code using constant method.
Use switch statement to get for the product price.
Include 6% tax calculation in the grand total.
Here is my answer, it is run with no error when I compile the code, but when I try to run it, it has the problem stated :
Run-Time Check Failure #3 - The variable 'pro1' is being used without being initialized.
My answer code :
#include <stdio.h>
int main()
{
float pro1, pro2, pro3, pro4, pro5;
float subtotal, Tax, total;
int unit;
char code;
int x, y, z, a, b;
printf("Enter the quantity of the product.\n");
scanf("%d", &unit);
printf("Enter the product code.(x=12345,y=56789,z=45678,a=13579,b=34567)\n");
scanf("%d", &code);
switch (code)
{
case 'x':
pro1 = unit*3.50;
break;
case 'y':
pro2 = unit*3.50;
break;
case 'z':
pro3 = unit*1.20;
break;
case 'a':
pro4 = unit*4.50;
break;
case 'b':
pro5 = unit*1.00;
break;
default:
printf("Invalid code.\n");
break;
}
printf("Enter the quantity of the product.\n");
scanf("%d", &unit);
printf("Enter the product code.(x=12345,y=56789,z=45678,a=13579,b=34567)\n");
scanf("%d", &code);
switch (code)
{
case 'x':
pro1 = unit*3.50;
break;
case 'y':
pro2 = unit*3.50;
break;
case 'z':
pro3 = unit*1.20;
break;
case 'a':
pro4 = unit*4.50;
break;
case 'b':
pro5 = unit*1.00;
break;
default:
printf("Invalid code.\n");
break;
}
printf("Enter the quantity of the product.\n");
scanf("%d", &unit);
printf("Enter the product code.(x=12345,y=56789,z=45678,a=13579,b=34567)\n");
scanf("%d", &code);
switch (code)
{
case 'x':
pro1 = unit*3.50;
break;
case 'y':
pro2 = unit*3.50;
break;
case 'z':
pro3 = unit*1.20;
break;
case 'a':
pro4 = unit*4.50;
break;
case 'b':
pro5 = unit*1.00;
break;
default:
printf("Invalid code.\n");
break;
}
printf("Enter the quantity of the product.\n");
scanf("%d", &unit);
printf("Enter the product code.(x=12345,y=56789,z=45678,a=13579,b=34567)\n");
scanf("%d", &code);
switch (code)
{
case 'x':
pro1 = unit*3.50;
break;
case 'y':
pro2 = unit*3.50;
break;
case 'z':
pro3 = unit*1.20;
break;
case 'a':
pro4 = unit*4.50;
break;
case 'b':
pro5 = unit*1.00;
break;
default:
printf("Invalid code.\n");
break;
}
printf("Enter the quantity of the product.\n");
scanf("%d", &unit);
printf("Enter the product code.(x=12345,y=56789,z=45678,a=13579,b=34567)\n");
scanf("%d", &code);
switch (code)
{
case 'x':
pro1 = unit*3.50;
break;
case 'y':
pro2 = unit*3.50;
break;
case 'z':
pro3 = unit*1.20;
break;
case 'a':
pro4 = unit*4.50;
break;
case 'b':
pro5 = unit*1.00;
break;
default:
printf("Invalid code.\n");
break;
}
printf("Artline 500A Black (Whiteboard Marker): %f", pro1);
printf("Artline 500A Red (Whiteboard Marker): %f", pro2);
printf("Pocket File (Yellow): %f", pro3);
printf("Pencil Casing: RM %f", pro4);
printf("A4 Exercise Book: %f", pro5);
subtotal = pro1 + pro2 + pro3 + pro4 + pro5;
Tax = subtotal*0.06;
total = subtotal + Tax;
printf("Subtotal : %f", subtotal);
printf("Tax : %f", Tax);
printf("Total : %f", total);
system("pause");
return 0;
}
I have only learned so far about basic I/O operations, if-else statements and switch case statements.
Imagine all your switch statements go through the defaultcase, and you hit the printf() with pro.
Certainly a read before write scenario for an uninitialized automatic local variable will cause undefined behavior in your application, as the value your're trying to read is indeterminate.
Suggestion: Always initialize your local variables.
What value has pro1 if the user never gave x as the code? Then pro1 is never assigned a value--which in C lingo means it is uninitialized when the control reaches
subtotal = pro1 + pro2 + pro3 + pro4 + pro5;
If the intent is to use 0 for products that aren't used, you should initialize them with
float pro1 = 0, pro2 = 0, pro3 = 0, pro4 = 0, pro5 = 0;
Important: there's another bug in
char code;
scanf("%d", &code);
code must be an int if you scan with a %d format. Turn on all the warnings of your compiler! In addition, you must test the return of scanf with
if (scanf("%d", &code) != 1) {
/* error: could not convert the input to a number. */
}
Not checking the return from scanf is a sure recipe for surprises.
There are many issues with your code:
You are violating the DRY principle in a way that is actually exagerated.
You never check the return value from scanf() that could lead to the variable keeping it's previous value or being uninitialized when you use it.
You print the value of the probN variables but it might happen that the user never inputs the code for the Nth product.
You should initialize them to 0, because if the value was never inserted by the user it means that "no units" were sold.
Your switch statement will never work, if you input x with the "%d" specifier the second point of these 4 points will be a problem because code will be uninitialized when you reach the switch, you should use the "%c" specifier instead and check the value returned by scanf() to be sure that something was scanned.
So instead of
scanf("%d", &code);
you need
if (scanf(" %c", &code) != 1)
wow_this_is_really_unexpected_but_I_am_glad_to_be_careful();
it's also wrong to pass a char * when using the "%d" specifier, and it's actually a serious bug.
Initialize your variables,
float pro1=0, pro2=0, pro3=0, pro4=0, pro5=0;
It can happen that these variables are used without any value set during the switch statements if it falls through default case.
I'm currently taking the Video Game Programming course at the Art Institute of Vancouver, in my second quarter and I have a programming class. I'm currently stuck on a bit of homework and I'm unsure why. I'm a beginner when it comes to programming. I have experience with HTML, CSS and Javascript.
The problem I have involves creating a bill for a telephone service customer, but it is entirely dependant on the type of service they are using. The code needs to be executed if the service code they input is either 'r' or 'p', and any other input results in an error. However I'm unsure where to proceed after this.
`
int account, minutes1, minutes2;
char s;
float cost;
printf("Please enter your account number. \n");
scanf_s("%d" , &account);
if(account == -1)
return 0;
printf("Please enter your service code(r for regular or p for premium). \n"); /*checking service*/
scanf_s(" %s" , &s);
switch(s)
{ /*execute specific parameters when r or p are selected as service codes.*/
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o': printf("Error. Please choose a correct service code (r for regular or p for premium). \n");
break;
case 'p': printf("Please enter how many minutes you've used between 6am and 6pm. \n");
scanf_s("%d" , &minutes1);
if(minutes1 > 75)
minutes1 = (minutes1-75)*.10;
printf("Please enter how many minutes you've used between 7pm and 5am. \n");
scanf_s("%d" , &minutes2);
if(minutes2 > 100)
minutes2 = (minutes2-100)*.05;
cost = 25+minutes1+minutes2;
printf("Your account number is %d, your service code is premium, you used %d minutes during the day and %d minutes during the night and your bill comes to $%lf. \n", account, minutes1, minutes2, cost);
break;
case 'q':
case 'r': printf("Please enter how many minutes you've used. \n");
scanf_s("%d", &minutes1);
if(minutes1 > 55)
minutes1 = (minutes1-55)*.20;
cost = 10+minutes1;
printf("Your account number is %d, your service code is regular, you used %d minutes and your bill comes to %lf. \n");
break;
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z': printf("Error. Please choose a correct service code (r for regular or p for premium). \n");
break;
}`
If you are scanning for character you should use scanf("%c",&s);
Also just use:
switch(s){
case 'p':
...
break;
case 'r':
...
break;
default:
error
}
How can I make a switch-case statement to not be case sensitive?
Say I made something like this:
#include <stdio.h>
char choice;
int main ()
{
char choice;
printf("Will you choose A,B, or C?\n>");
scanf(" %c", &choice);
switch(choice)
{
case 'A':
printf("The First Letter of the Alphabet");
break;
case 'B':
printf("The Second Letter of the Alphabet");
break;
case 'C':
printf("The Third Letter of the Alphabet");
break;
}
}
It would only respond to capital letters. How do I make it respond to lower case letters?
toupper in <ctype.h> converts a character to uppercase:
#include <stdio.h>
#include <ctype.h>
char choice;
int main ()
{
printf("Will you choose A,B, or C?\n>");
scanf(" %c", &choice);
switch(toupper(choice)) // Changed line
{
case 'A':
printf("The First Letter of the Alphabet");
break;
case 'B':
printf("The Second Letter of the Alphabet");
break;
case 'C':
printf("The Third Letter of the Alphabet");
break;
}
You simply need this :-
switch(choice)
{
case 'A':
case 'a':
printf("The First Letter of the Alphabet");
break;
case 'B':
case 'b':
printf("The Second Letter of the Alphabet");
break;
case 'C':
case 'c':
printf("The Third Letter of the Alphabet");
break;
}
and so on to continue your series.
Actually,what it does is that it bypasses(skims) upto bottom until it finds the first break statement matching the case thereby executing all the cases encountered in between!!!
Before the switch(), add:
choice = toupper(choice);
And if you haven't already got it, #include <ctype.h> to get the prototype.
You can give 2 cases one by one,
switch(choice)
{
case 'A':
case 'a':
printf("The First Letter of the Alphabet");
break;
case 'B':
case 'b':
printf("The Second Letter of the Alphabet");
break;
case 'C':
case 'c':
printf("The Third Letter of the Alphabet");
break;
}