So, keep having problems with this calculator I'm making (Still learning C), it's giving me an error at the very end of the code, 'Expected }'. But I have } at the end, Ive added another one to see if that would work, but then it says 'Expected while in do/while loop'.
#include <stdio.h>
#include <stdbool.h>
int main()
{
float number[100];
int operator = '0';
int doAgainAnswer = 0;
int doAgain;
float finished;
int error;
do{
if(number[1] == 0){
printf("Number 1: ");
scanf("%f", &number[1]);
} else if(number[1] != 0) {
printf("\n\nNumber 1: %.2f\n", number[1]);
printf("Number 2: ");
scanf("%f", &number[2]);
printf("\nOperator:\n1 Addition\n2 Subract\n3 Multiply\n4 Divide.\nChoice: ");
scanf("%d", &operator);
if(operator == 1){
finished = number[1] + number[2];
printf("\n\n%.2f \+ %.2f is: %.2f", number[1], number[2], finished);
}
if(operator == 2){
finished = number[1] - number[2];
printf("\n\n%.2f \- %.2f is: %.2f", number[1], number[2], finished);
}
if(operator == 3){
finished = number[1] * number[2];
printf("\n\n%.2f \* %.2f is: %.2f", number[1], number[2], finished);
}
if(operator == 4){
finished = number[1] / number[2];
printf("\n\n%.2f \/ %.2f is: %.2f", number[1], number[2], finished);
}
if(operator > 4){
printf("\n\nERROR: Invalid operation.\n\n ");
error = '1';
}
if(error != 1){
printf("\nContinue?\n1: Yes\n2: Yes and use answer as starting value.\n3: No\nAnswer: ");
} else if(error == 1){
printf("\nTry again? 1 Yes 3 No: ");
}
scanf("%d", &doAgainAnswer);
if(doAgainAnswer == 1) {
doAgain = 1;
} else
if(doAgainAnswer == 2){
doAgain = 1;
number[1] = finished;
} else
if(doAgainAnswer == 3){
doAgain = 0;
printf("Goodbye :(");
exit(0);
} else
if(doAgainAnswer > 4){
printf("\n\nERROR INVALID OPERATION.\n\n");
}
}while(doAgain == 1);
return 0;
}
It looks like this part of your code contains an else which is never closed, i.e.:
if(number[1] == 0)
{
printf("Number 1: ");
scanf("%f", &number[1]);
}
else if(number[1] != 0)
{
....
} <-- missing.
Whole code block, reformatted slightly differently:
#include <stdio.h>
#include <stdbool.h>
int main()
{
float number[100];
int operator = '0';
int doAgainAnswer = 0;
int doAgain;
float finished;
int error;
do
{
if(number[1] == 0)
{
printf("Number 1: ");
scanf("%f", &number[1]);
}
else if(number[1] != 0)
{
printf("\n\nNumber 1: %.2f\n", number[1]);
printf("Number 2: ");
scanf("%f", &number[2]);
printf("\nOperator:\n1 Addition\n2 Subract\n3 Multiply\n4 Divide.\nChoice: ");
scanf("%d", &operator);
if(operator == 1)
{
finished = number[1] + number[2];
printf("\n\n%.2f \+ %.2f is: %.2f", number[1], number[2], finished);
}
if(operator == 2)
{
finished = number[1] - number[2];
printf("\n\n%.2f \- %.2f is: %.2f", number[1], number[2], finished);
}
if(operator == 3)
{
finished = number[1] * number[2];
printf("\n\n%.2f \* %.2f is: %.2f", number[1], number[2], finished);
}
if(operator == 4)
{
finished = number[1] / number[2];
printf("\n\n%.2f \/ %.2f is: %.2f", number[1], number[2], finished);
}
if(operator > 4)
{
printf("\n\nERROR: Invalid operation.\n\n ");
error = '1';
}
if(error != 1)
{
printf("\nContinue?\n1: Yes\n2: Yes and use answer as starting value.\n3: No\nAnswer: ");
}
else if(error == 1)
{
printf("\nTry again? 1 Yes 3 No: ");
}
scanf("%d", &doAgainAnswer);
if(doAgainAnswer == 1)
{
doAgain = 1;
}
else if(doAgainAnswer == 2)
{
doAgain = 1;
number[1] = finished;
}
else if(doAgainAnswer == 3)
{
doAgain = 0;
printf("Goodbye :(");
exit(0);
}
else if(doAgainAnswer > 4)
{
printf("\n\nERROR INVALID OPERATION.\n\n");
}
}
}
while(doAgain == 1);
return 0;
}
else if(number[1] != 0) {
printf("\n\nNumber 1: %.2f\n", number[1]);
You never close this else if statement.
To prevent errors like this from happening, I suggest that you use a different kind of indentation.
If you write your code like this:
int main()
{
float number[100];
int operator = '0';
int doAgainAnswer = 0;
int doAgain;
float finished;
int error;
do
{
if(number[1] == 0)
{
printf("Number 1: ");
scanf("%f", &number[1]);
}
else if(number[1] != 0)
{
printf("\n\nNumber 1: %.2f\n", number[1]);
printf("Number 2: ");
scanf("%f", &number[2]);
printf("\nOperator:\n1 Addition\n2 Subract\n3 Multiply\n4 Divide.\nChoice: ");
scanf("%d", &operator);
You should notice instantly that there's a }missing after the else if statement.
Try having writing the {} in newlines and having them with no code in that line. it will help you get organized and find problems easier!
Proper indentation shows that you are missing a } to match your first else if {.
You really need to use a proper editor (one that at least does auto-indentation), use {} around your elses to avoid dangling them*, and use helper functions.
#include <stdio.h>
#include <stdbool.h>
int main()
{
...
do{
if(number[1] == 0){
printf("Number 1: ");
scanf("%f", &number[1]);
} else if(number[1] != 0) {
...
}while(doAgain == 1);
return 0;
}
*: seriously, always use braces unless it's short enough to go on a single line (else return, or similar). I found this in code at my last company:
if(DEBUG)
// print(foo);
if(something_important) {
...
}
Your code needs better indentation. With proper indentation it'd be easier to spot where the is (or is not) the missing bracket. You didn't close the outer if/else statement, right before the while condition:
#include <stdio.h>
#include <stdbool.h>
int main()
{
float number[100];
int operator = '0';
int doAgainAnswer = 0;
int doAgain;
float finished;
int error;
do {
if(number[1] == 0) {
printf("Number 1: ");
scanf("%f", &number[1]);
}
else if (number[1] != 0) {
printf("\n\nNumber 1: %.2f\n", number[1]);
printf("Number 2: ");
scanf("%f", &number[2]);
printf("\nOperator:\n1 Addition\n2 Subract\n3 Multiply\n4 Divide.\nChoice: ");
scanf("%d", &operator);
if (operator == 1) {
finished = number[1] + number[2];
printf("\n\n%.2f \+ %.2f is: %.2f", number[1], number[2], finished);
}
if (operator == 2) {
finished = number[1] - number[2];
printf("\n\n%.2f \- %.2f is: %.2f", number[1], number[2], finished);
}
if (operator == 3) {
finished = number[1] * number[2];
printf("\n\n%.2f \* %.2f is: %.2f", number[1], number[2], finished);
}
if (operator == 4) {
finished = number[1] / number[2];
printf("\n\n%.2f \/ %.2f is: %.2f", number[1], number[2], finished);
}
if (operator > 4) {
printf("\n\nERROR: Invalid operation.\n\n ");
error = '1';
}
if (error != 1) {
printf("\nContinue?\n1: Yes\n2: Yes and use answer as starting value.\n3: No\nAnswer: ");
}
else if (error == 1) {
printf("\nTry again? 1 Yes 3 No: ");
}
scanf("%d", &doAgainAnswer);
if (doAgainAnswer == 1) {
doAgain = 1;
}
else if(doAgainAnswer == 2) {
doAgain = 1;
number[1] = finished;
}
else if (doAgainAnswer == 3) {
doAgain = 0;
printf("Goodbye :(");
exit(0);
}
else if (doAgainAnswer > 4) {
printf("\n\nERROR INVALID OPERATION.\n\n");
}
/**
* Missing a bracket here, to close the if/else statement
*/
}
while (doAgain == 1);
return 0;
}
// Indentation should end at this level
Related
I am just making a simple calculator and would like to know
how could I make my calculator loop back to the start when an invalid operator/float is entered; to ask the user to input everything again. Basically restarting it.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
double num1;
double num2;
char op;
printf("Input a number: ");
scanf("%lf", &num1);
printf("Enter operator (+,-,*,/,^): ");
scanf(" %c", &op);
printf("Enter the second number: ");
scanf(" %lf", &num2);
if (op == '+')
{
printf("%f", num1 + num2);
}
else if (op == '-')
{
printf("%f", num1 - num2);
}
else if (op == '/')
{
printf("%f", num1 / num2);
}
else if (op == '*')
{
printf("%f", num1 * num2);
}
else if (op == '^')
{
printf("%f", pow(num1,num2));
}
else
{
printf("Invalid operator entered");
}
return 0;
}
I would do it another way to avoid \n scanf problems
#define MAXNUMSTR 100
int calc(void)
{
char line[MAXNUMSTR];
double num1;
double num2;
double res;
do
{
printf("\nInput first number: ");
if(!fgets(line, MAXNUMSTR -1, stdin)) return 0;
}while(sscanf(line, "%lf", &num1) != 1);
do
{
printf("\nInput second number: ");
if(!fgets(line, MAXNUMSTR -1, stdin)) return 0;
}while(sscanf(line, "%lf", &num2) != 1);
printf("\nEnter operator (+,-,*,/,^, x): ");
if(!fgets(line, MAXNUMSTR -1, stdin)) return 0;
switch(line[0])
{
case '+':
res = num1 + num2;
break;
case '-':
res = num1 - num2;
break;
case '*':
res = num1 * num2;
break;
case '/':
if(num2 == 0.0)
{
printf("\nDivision by zero\n");
return 1;
}
res = num1 / num2;
break;
case '^':
res = pow(num1, num2);
break;
case 'x':
return 0;
default:
printf("\nInvalid operation\n");
return 1;
}
printf("\n%f %c %f = %f\n", num1, num2, res);
return 1;
}
int main(void)
{
while(calc());
return 0;
}
Insert the whole logic in an infinite loop like below.
while(1) {
printf("Input a number: ");
scanf("%lf", &num1);
printf("Enter operator (+,-,*,/,^): ");
scanf(" %c", &op);
printf("Enter the second number: ");
scanf(" %lf", &num2);
if (op == '+')
{
printf("%f", num1 + num2);
}
else if (op == '-')
{
printf("%f", num1 - num2);
}
else if (op == '/')
{
printf("%f", num1 / num2);
}
else if (op == '*')
{
printf("%f", num1 * num2);
}
else if (op == '^')
{
printf("%f", pow(num1,num2));
}
else
{
printf("Invalid operator entered");
}
}
Basically this is a program to play with dice but i have a problem with srand. it gives an error stating "implicit conversion loses integer precision: 'time_t' (aka 'long') to 'unsigned int'"
whys that ?
Also i would like to know if there are any other ways to improve this code? currently i only know stdio.h i know there are some out there like iostream or something like that. is there a possible way to explain those to me also.
Here is the code.
i know crt secure no warning is to ignore some errors but i would still like to know why it the error pops up ?
#define _CRT_SECURE_NO_WARNINGS
#define MAX_CHIPS 400
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void display_details(void);
int throws_die(void);
int main() so on
{
int die1, die2, throw_sum;
int PointToMake;
int Games_Played = 0;
int Games_Won = 0;
int Games_Lost = 0;
int CurrentChips = MAX_CHIPS;
int BetChips;
char PlayerName[30];
char PlayAgain;
display_details();
srand(time(NULL));
printf("Would you like to play Test your luck [y|n]?\n");
scanf(" %c", &PlayAgain);
while ((PlayAgain == 'y') && (CurrentChips > 0))
{
if (Games_Played == 0)//shows how many games played which is obviously 0
{
printf("A new chellenger! What is your name?\n");
scanf(" %s", &PlayerName);
printf("Hi %s!\n", PlayerName);
}
Games_Played = Games_Played + 1;//each new game it will increase by 1
printf("Number of chips: %d\n", CurrentChips);
printf("Place your bet:\n ");
scanf("%d", &BetChips);
while ((BetChips < 0) || (BetChips > CurrentChips))
{
printf("uuh ....You can only bet the chips you have.. (0-%d)!\n", CurrentChips);
printf("Place your bet: \n");
scanf("%d", &BetChips);
}
die1 = throws_die();
die2 = throws_die();
throw_sum = die1 + die2;
printf("You Got: %d + %d = %d\n", die1, die2, throw_sum);
if ((throw_sum == 7) || (throw_sum == 12))
{
//+1 to games won
Games_Won = Games_Won + 1;
//adding to bet balance
CurrentChips = CurrentChips + BetChips;
printf("XXXXX Winner! d(^_^) XXXXX\n");
}
else if ((throw_sum == 2) || (throw_sum == 3) || (throw_sum == 10))
{
Games_Lost = Games_Lost + 1;
CurrentChips = CurrentChips - BetChips;
printf("XXXXX Loser! :( XXXXX\n");
}
else
{
PointToMake = throw_sum;
printf("Points to make is: %d\n", PointToMake);
die1 = throws_die();
die2 = throws_die();
throw_sum = die1 + die2;
printf(" |--->> Spinned: %d + %d = %d\n", die1, die2, throw_sum);
while (throw_sum != PointToMake && throw_sum != 7)
{
die1 = throws_die();
die2 = throws_die();
throw_sum = die1 + die2;
printf(" |--->> Spinned: %d + %d = %d\n", die1, die2, throw_sum);
}
if (throw_sum == PointToMake)
{
printf("XXXXX Winner! (x2 the bet) XXXXX\n");
//x2 added to balance
CurrentChips = CurrentChips + 2 * BetChips;
Games_Won = Games_Won + 1;
}
else
{
Games_Lost = Games_Lost + 1;
CurrentChips = CurrentChips - BetChips;
printf("XXXXX Loser!:( XXXXX\n");
}
}
if (CurrentChips > 0)
{
printf("You now have %d chips.\n", CurrentChips);
printf("============================\n\n");
printf("Play Again %s [y|n]? ", PlayerName);
scanf(" %c", &PlayAgain);
printf("============================\n");
}
else
{
printf("you're out of chips.\n");
printf("XXXXX GG TRY AGAIN NEXT TIME XXXXX\n");
}
}
if (Games_Played == 0)
{
printf("Oh well.... better luck next time!\n");
}
else if (Games_Played == 1)
{
printf("%s played %d game and is cashing out with %d chips!\n", PlayerName, Games_Played, CurrentChips);
printf(" |---> Games won: %d\n", Games_Won);
printf(" |---> Games lost: %d\n", Games_Lost);
printf("Thanks for playing, come again to test that luck of yours %s.\n", PlayerName);
}
else
{
printf("%s played %d games and is cashing out with %d chips!\n", PlayerName, Games_Played, CurrentChips);
printf(" |---> Games won: %d\n", Games_Won);
printf(" |---> Games lost: %d\n", Games_Lost);
printf("\nThanks for playing, come again to test that luck of yours %s!\n", PlayerName);
}
return 0;
}
void display_details(void)
{
printf("File :.... assignment.c\n");
printf("Author :.... \n");
printf("Stud ID :.... \n");
printf("Email ID :.... \n");
printf("This is my own work as defined by the \n");
printf("University's Academic Misconduct Policy.\n");
}
int throws_die(void)
{
return 1 + rand() % 6;
srand is defined more or less like this:
void srand(unsigned int seed);
time is defined more or less like this:
__int64 time(__int64 *t);
Thus you pass an __int64 (64 bits) into an unsigned int (32 bits) parameter, which is not possible without potential trunctation. That's what the warning message is telling you.
I created a c program in visual studio 2015 and I made a setup file
the setup file works fine in my computer . So i decided to sent the setup file to my friend , though the setup file was installed successfully when we run the program it shows system error "the program can't star because VCRUNTIME140D.dll is missing from your computer.Try re-installing the program"
here is the program I wrote ( which works perfectly )
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
void beg();
void beg2();
void beg3();
void beg4();
void beg5();
void beg6();
void beg7();
void beg8();
void beg9();
void beg10();
void store_a();
void the_end();
void wrong();
void right();
void help();
int i,m;
char b[101],a[101];
void main()
{
int diff;
i = 0;
printf("\n WELCOME THO THE GAME \n"); //welcome
printf(" ARE YOU READY !!\n");
do
{
printf("\n 1.play\t 2.help\t 3.exit\n"); //menu
printf(" Enter your option : ");
scanf(" %d", &diff);
switch (diff)
{
case 1:
{
beg();
break;
}
case 2:
{
help();
break;
}
case 3:
{
break;
}
}
} while (diff != 3);
}
void beg()
{
int b;
m = 2;
printf("\n what is the capital of india?\n");
printf(" options\n");
printf(" 1.New Delhi\n 2.Mumbai\n 3.Kochi\n 4.Chennai\n");
scanf("%d", &b);
if (b == 1)
{
right();
beg2();
}
else
{
wrong();
beg2();
}
}
void beg2()
{
int b;
m = 3;
printf("\n 12 + 11 = ?\n");
printf(" options\n");
printf(" 1.12\n 2.45\n 3.23\n 4.17\n");
scanf("%d", &b);
if (b != 3)
{
b = 2;
wrong();
beg3();
}
else
{
while (b == 3)
{
if (b == 3)
{
right();
beg3();
break;
}
}
}
}
void beg3()
{
int b;
m = 4;
printf("\n who is the prime minister of india ?\n");
printf(" options\n");
printf(" 1.Pranab Mukherjee\n 2.narendra modi\n 3.manmohan singh\n 4.steven thomas\n");
scanf("%d", &b);
if (b != 2)
{
b = 1;
wrong();
beg4();
}
else
{
while (b == 2)
{
if (b == 2)
{
right();
beg4();
break;
}
}
}
}
void beg4()
{
int b;
m = 5;
printf("\n 5 x 7 = ?\n");
printf(" options\n");
printf(" 1.32\n 2.54\n 3.25\n 4.35\n");
scanf("%d", &b);
if (b != 4)
{
b = 1;
wrong();
beg5();
}
else
{
while (b == 4)
{
if (b == 4)
{
right();
beg5();
break;
}
}
}
}
void beg5()
{
int b;
m = 6;
printf("\n which country is paris the capital of?\n");
printf(" options\n");
printf(" 1.India\n 2.France\n 3.England\n 4.China\n");
scanf("%d", &b);
if (b != 2)
{
b = 1;
wrong();
beg6();
}
else
{
while (b == 2)
{
if (b == 2)
{
right();
beg6();
break;
}
}
}
}
void beg6()
{
int b;
m = 7;
printf("\n what is the capital of america?\n");
printf(" options\n");
printf(" 1.Los Angeles\n 2.New York \n 3.Washington, D.C.\n 4.Las Vegas\n");
scanf("%d", &b);
if (b != 3)
{
b = 1;
wrong();
beg7();
}
else
{
while (b == 3)
{
if (b == 3)
{
right();
beg7();
break;
}
}
}
}
void beg7()
{
int b;
m = 8;
printf("\n what is the highest mountain in the world?\n");
printf(" options\n");
printf(" 1.Mount Everest\n 2.Kangchenjunga \n 3.Annapurna\n 4.K2 \n");
scanf("%d", &b);
if (b != 1)
{
b = 3;
wrong();
beg8();
}
else
{
while (b == 1)
{
if (b == 1)
{
right();
beg8();
break;
}
}
}
}
void beg8()
{
int b;
m = 9;
printf("\n who invented piano?\n");
printf(" options\n");
printf(" 1.serin thomas\n 2.Bartolomeo Cristofori \n 3.annrose pino\n 4.Baldwin pinero \n");
scanf("%d", &b);
if (b != 2)
{
b = 3;
wrong();
beg9();
}
else
{
while (b == 2)
{
if (b == 2)
{
right();
beg9();
break;
}
}
}
}
void beg9()
{
int b;
m = 10;
printf("\n what is the answer of ( 3-3 x 6+2 )?\n");
printf(" options\n");
printf(" 1.-17\n 2. 0 \n 3. -13\n 4. 9 \n");
scanf("%d", &b);
if (b != 3)
{
b = 1;
printf("\n Do you want to know the correct answer for the qustion :\n1.yes\n2.no\t: ");
scanf("%d", &opt);
if (opt == 1)
{
printf("\n The correct answer is as follows:\n Question: 3 – 3 x 6 + 2\n Multiplication first : 3 – 18 + 2\n Left to right : -15 + 2\n Answer : -13\n");
}
wrong();
beg10();
}
else
{
while (b == 3)
{
if (b == 3)
{
right();
beg10();
break;
}
}
}
}
void beg10()
{
int len,i,f;
store_a();
printf("\n A man has a barrel with filled with oil that weighs 100 pounds,\n and then he puts something into it.\n Now the barrel weighs less than 100 pounds.\n What did he put in the barrel ?? \n");
printf(" enter the answer :");
scanf("%s", b);
len = strlen(b);
for (i = 0; i < len; i++)
{
if (a[i] == b[i])
{
f = 1;
}
else
{
f = 0;
break;
}
}
if (f == 1)
{
printf("The answer is correct!!\n");
the_end();
}
else
{
printf(" Wrong answer\nthe correct ans was 'hole'\n");
the_end();
}
}
void wrong()
{
printf("wrong answer !!\n");
printf("your score is :%d\n", i);
printf("\n qustion number: %d", m);
}//when the option is wrong
void the_end()
{
printf("\n***congratulations***\n");
printf(" your final score is :%d\n", i);
}//at the end of the game
void right()
{
printf("the answer is correct !!\n");
i++;
printf("your score is :%d\n", i);
printf("\n qustion number: %d", m);
}//when the ans is correct
void help()
{
int opt;
printf("\n 1.How to play\t2.About\t3.Main menu\n");
printf(" enter your option : ");
scanf("%d", &opt);
if (opt == 1)
{
printf(" answer the qustions..\n you get 1 point for each qustion..\n there is a total of 10 qustions try to get the maximum marks\n good luck \n");
help();
}
else if (opt == 2)
{
printf("\tCREATED BY\n **STEVEN THOMAS** \n **steventhomaspuli#gmail.com**\n ");
help();
}
else if (opt == 3)
{
main();
}
}//help menu
void store_a()
{
a[0] = 'h';
a[1] = 'o';
a[2] = 'l';
a[3] = 'e';
}
and this is the setup file I created
click here to download
pls tell me what is wrong here
You had built and shipped debug version of your program. Debug VC runtime is not a part of VC redistributable package, so your friend get a message about missing DLLs. Build and ship release version.
Basically I am supposed to simulate a program that rolls three dice, add it up. Then I will have the user guess if the next roll is higher, lower, same or if they just want to quit. I am having two problems.
The numbers are not random, obviously this is a big mistake on my part but I can't seem to figure it out. I am thinking I do not need the second inclusion of 3 dice pairs? They don't help at all anyways.
Regardless, my program goes through all of the if/else if statements all at once. Obviously I do not want this to happen.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int diceOne, diceTwo, diceThree, diceSum=0, timesCorrect=0, choice;
int newDiceOne, newDiceTwo, newDiceThree, newDiceSum;
srand( time(NULL) );
diceOne = rand() % 6 + 1;
diceTwo = rand() % 6 + 1;
diceThree = rand() % 6 + 1;
newDiceOne = rand() % 6 + 1;
newDiceTwo = rand() % 6 + 1;
newDiceThree = rand() % 6 + 1;
printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
diceSum = diceOne + diceTwo + diceThree;
printf("\nTotal sum of dice is %d\n", diceSum);
do {
printf("Guess higher(1), lower(2), same(3) or quit?(4)\n");
printf(" You have been correct %d times\n", timesCorrect);
scanf("%d", &choice);
printf("The three dice rolls: %d, %d, %d", newDiceOne, newDiceTwo, newDiceThree);
newDiceSum= newDiceOne + newDiceTwo + newDiceThree;
printf("\nTotal sum of dice is %d\n", newDiceSum);
if (choice == 1)
{
if (newDiceSum > diceSum);
timesCorrect++;
printf("You are correct!\n");
}
else if (newDiceSum < diceSum);
{
printf("You are incorrect, sorry!\n");
}
if (choice == 2)
{
if (newDiceSum < diceSum);
timesCorrect++;
printf("You are correct!\n");
}
else if (newDiceSum > diceSum);
{
printf("You are incorrect, sorry!\n");
}
if (choice == 3)
{
if (newDiceSum == diceSum);
timesCorrect ++;
printf("You are correct!\n");
}
else if (newDiceSum != diceSum);
{
printf("You are incorrect, sorry!\n");
}
if (choice == 4)
{
printf("Thanks for playing!!!!!!\n");
system("pause");
return 0;
}
} while (choice!= 4 );
}
You have an extra semicolon after the else if conditionals, like here
else if (newDiceSum < diceSum);
/* ^ this should not be here */
If you use a compiler with good diagnostic capabilities and enable warnings, it should warn you about that "typo", if you want to leave the block empty use braces like
else if (newDiceSum < diceSum) {}
Also, you set the first dice with rand() and they are random values, but then you always use the same values in the loop.
The following code:
Handles error conditions
Eliminates unneeded variables
Cleanly compiles
Executes with out failing
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ( void )
{
int diceOne;
int diceTwo;
int diceThree;
int diceSum=0;
int timesCorrect=0;
int choice;
int newDiceSum;
srand( time(NULL) );
diceOne = rand() % 6 + 1;
diceTwo = rand() % 6 + 1;
diceThree = rand() % 6 + 1;
printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
diceSum = diceOne + diceTwo + diceThree;
printf("\nTotal sum of dice is %d\n", diceSum);
do {
printf("Guess higher(1), lower(2), same(3) or quit?(4)\n");
printf(" You have been correct %d times\n", timesCorrect);
if( 1 != scanf("%d", &choice) )
{ // then scanf failed
perror( "scanf for choice failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
diceOne = rand() % 6 + 1;
diceTwo = rand() % 6 + 1;
diceThree = rand() % 6 + 1;
printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
newDiceSum = diceOne + diceTwo + diceThree;
printf("\nTotal sum of dice is %d\n", newDiceSum);
switch( choice )
{
case 1:
if (newDiceSum > diceSum)
{
timesCorrect++;
printf("You are correct!\n");
}
else
{
printf("You are incorrect, sorry!\n");
}
break;
case 2:
if (newDiceSum < diceSum)
{
timesCorrect++;
printf("You are correct!\n");
}
else
{
printf("You are incorrect, sorry!\n");
}
break;
case 3:
if (newDiceSum == diceSum)
{
timesCorrect ++;
printf("You are correct!\n");
}
else
{
printf("You are incorrect, sorry!\n");
}
break;
case 4:
printf("Thanks for playing!!!!!!\n");
system("pause");
break;
default:
printf( " invalid choice, valid choices are 1...4\n");
break;
} // end switch
} while (choice!= 4 );
return(0);
} // end function: main
I'm learning C, and I'm trying to make a basic calculator, but having some trouble with the while statements. I've tried doing it multiple ways but it never repeats itself, just finishes the script.
Any ideas?
//
// main.c
// Calculator
//
// Created by Austen Patterson on 2013-06-27.
// Copyright (c) 2013 Austen Patterson. All rights reserved.
//
#include <stdio.h>
#include <stdbool.h>
int main()
{
int number[100];
int operator = '0';
int doAgainAnswer = '0';
bool doAgain;
do{
printf("Please enter your first number:");
scanf("%d", &number[1]);
printf("\nYou entered %d as your first number. Please enter your second: ", number[1]);
scanf("%d", &number[2]);
printf("\nYou entered %d as your second number.", number[2]);
printf("\nYour numbers are now %d and %d", number[1], number[2]);
printf("\nNow enter your operator.\n1 for addition\n2 for subraction\n3 for multiplication\n4 for division.\n");
scanf("%d", &operator);
if(operator == 1){
int finished = number[1] + number[2];
printf("\n\n%d \+ %d is: %d", number[1], number[2], finished);
}
if(operator == 2){
int finished = number[1] - number[2];
printf("\n\n%d \- %d is: %d", number[1], number[2], finished);
}
if(operator == 3){
int finished = number[1] * number[2];
printf("\n\n%d \* %d is: %d", number[1], number[2], finished);
}
if(operator == 4){
int finished = number[1] / number[2];
printf("\n\n%d \/ %d is: %d", number[1], number[2], finished);
}
printf("\nWant to continue?\n 1 for Yes\n 2 for No\nAnswer: ");
scanf("%d", &doAgainAnswer);
if(doAgainAnswer == 1) {
doAgain = '1';
} else {
doAgain = '0';
}
}while(doAgain == '1');
}
Edited code
#include <stdio.h>
#include <stdbool.h>
int main()
{
int number[100];
int operator = '0';
int doAgainAnswer = 0;//edited
int doAgain=0;//edited
do{
printf("Please enter your first number:");
scanf("%d", &number[1]);
printf("\nYou entered %d as your first number. Please enter your second: ", number[1]);
scanf("%d", &number[2]);
printf("\nYou entered %d as your second number.", number[2]);
printf("\nYour numbers are now %d and %d", number[1], number[2]);
printf("\nNow enter your operator.\n1 for addition\n2 for subraction\n3 for multiplication\n4 for division.\n");
scanf("%d", &operator);
if(operator == 1){
int finished = number[1] + number[2];
printf("\n\n%d \+ %d is: %d", number[1], number[2], finished);
}
if(operator == 2){
int finished = number[1] - number[2];
printf("\n\n%d \- %d is: %d", number[1], number[2], finished);
}
if(operator == 3){
int finished = number[1] * number[2];
printf("\n\n%d \* %d is: %d", number[1], number[2], finished);
}
if(operator == 4){
int finished = number[1] / number[2];
printf("\n\n%d \/ %d is: %d", number[1], number[2], finished);
}
printf("\nWant to continue?\n 1 for Yes\n 2 for No\nAnswer: ");
scanf("%d", &doAgainAnswer);
if(doAgainAnswer == 1) {
doAgain = 1;//edited
} else {
doAgain = 0;//edited
}
}while(doAgain == 1);//edited
return 0;//edited
}
Change the while Condition to
while(doAgain==1)
And in all the doAgain assingments, use 0 or 1.
Other way would be to use While(1) and break when user enters 0 at prompt in doAgainAnswer.