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.
Related
This is the while loop and I would like to stop the loop a character 'q' is typed as an input. So when "Please enter the five requirements" appears and I type in q then it stops running
while (1)
{
printf("Please enter the five requirements:");
scanf("%d %f %f %d %d", &my, &mii, &mr, &ml, &mp);
printf("You entered: %d %f %f %d %d\n", my, mii, mr, ml, mp);
for (i = 0; i < 15; i++)
{
if (my <= mortGage[i].min_d && mii >= mortGage[i].max_iin && mr >= mortGage[i].max_rt &&
ml <= mortGage[i].min_l && mp >= mortGage[i].max_pf)
{
printf("name is : %s\n", mortGage[i].name_b);
printf("min duration is : %d\n", mortGage[i].min_d);
printf("max initial rate is : %f\n", mortGage[i].max_iin);
printf("max rate is : %f\n", mortGage[i].max_rt);
printf("min loan is : %d\n", mortGage[i].min_l);
printf("max product fees is : %d\n", mortGage[i].max_pf);
printf("\n");
}
else
{
printf("");
}
//if (my == 'q'|| mii == 'q' || mr == 'q' || ml == 'q' || mp == 'q')
//break;
}
}
I'm not sure if it can help you. This is what I do here:
#include <stdio.h>
int main()
{
int check;
while (1)
{
printf("Please enter the five requirements:");
check=scanf("%d %f %f %d %d", &my, &mii, &mr, &ml, &mp);
if(checker==0){
printf("Input Q\n");
break;
}else{
printf("You entered: %d %f %f %d %d\n", my, mii, mr, ml, mp);
for (i = 0; i < 15; i++)
{
if (my <= mortGage[i].min_d && mii >= mortGage[i].max_iin && mr >= mortGage[i].max_rt &&
ml <= mortGage[i].min_l && mp >= mortGage[i].max_pf)
{
printf("name is : %s\n", mortGage[i].name_b);
printf("min duration is : %d\n", mortGage[i].min_d);
printf("max initial rate is : %f\n", mortGage[i].max_iin);
printf("max rate is : %f\n", mortGage[i].max_rt);
printf("min loan is : %d\n", mortGage[i].min_l);
printf("max product fees is : %d\n", mortGage[i].max_pf);
printf("\n");
}else{
printf("");
}
}
}
}
return 0;
}
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.
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
#include<stdio.h>
int main()
{
int choice;
printf("Enter 1 for Programmers Name and ID\n");
printf("Enter 2 to Perform Integer Operation\n");
printf("Enter 3 to Perform Floating Point Operation\n");
scanf("%d", &choice);
system("CLS");
if (choice == 1)
printf("Connor \n000000000\n");
else if (choice == 2)
{
char c;
int num1, num2;
printf("Enter operator:");
scanf("%c", &c);
getchar();
printf("Enter two integer's :");
scanf("%d %d", &num1, &num2);
switch (c)
{
case '+':
printf("%d + %d = %d", num1, num2, num1 + num2);
break;
case '-':
printf("%d - %d = %d", num1, num2, num1 - num2);
break;
case '*':
printf("%d * %d = %d", num1, num2, num1*num2);
break;
case '/':
printf("%d / %d = %d", num1, num2, num1 / num2);
break;
default:
printf("The value of c = '%c'\n");
system("pause");
return(0);
}
}
else if (choice == 3)
printf("Enter two \n");
system("pause");
return(0);
}
I need a little help figuring out a small problem with the operations part of this code.......everything works out as in can put in the operator and the integers but I do not get the output from the switch.
this should definitely work
#include<stdio.h>
int main()
{
int choice;
printf("Enter 1 for Programmers Name and ID\n");
printf("Enter 2 to Perform Integer Operation\n");
printf("Enter 3 to Perform Floating Point Operation\n");
scanf("%d", &choice);
getchar();
if (choice == 1)
printf("Connor \n000000000\n");
else if (choice == 2)
{
char c;
int num1, num2;
printf("Enter operator:");
scanf("%c", &c);
printf("Enter two integer's :");
scanf("%d %d", &num1, &num2);
switch (c)
{
case '+':
printf("%d + %d = %d", num1, num2, num1 + num2);
break;
case '-':
printf("%d - %d = %d", num1, num2, num1 - num2);
break;
case '*':
printf("%d * %d = %d", num1, num2, num1*num2);
break;
case '/':
printf("%d / %d = %d", num1, num2, num1 / num2);
break;
default:
printf("The value of c = '%c'\n" , c);
return(0);
}
}
else if (choice == 3)
printf("Enter two \n");
}
Change:
scanf("%c", &c);
getchar();
to:
scanf(" %c", &c);
Add an \n to each of these:
printf("%d + %d = %d\n", num1, num2, num1 + num2);
^^
Actually provide a char when you tell printf() to print one:
printf("The value of c = '%c'\n", c);
^^^
and it should work for you. Revised code, removing all the system() nonsense:
#include <stdio.h>
int main(void)
{
int choice;
printf("Enter 1 for Programmers Name and ID\n");
printf("Enter 2 to Perform Integer Operation\n");
printf("Enter 3 to Perform Floating Point Operation\n");
scanf("%d", &choice);
if ( choice == 1 ) {
printf("Connor \n000000000\n");
}
else if ( choice == 2 ) {
char c;
int num1, num2;
printf("Enter operator:");
scanf(" %c", &c);
printf("Enter two integers :");
scanf("%d %d", &num1, &num2);
switch ( c ) {
case '+':
printf("%d + %d = %d\n", num1, num2, num1 + num2);
break;
case '-':
printf("%d - %d = %d\n", num1, num2, num1 - num2);
break;
case '*':
printf("%d * %d = %d\n", num1, num2, num1 * num2);
break;
case '/':
printf("%d / %d = %d\n", num1, num2, num1 / num2);
break;
default:
printf("The value of c = '%c'\n", c);
break;
}
} else if ( choice == 3 ) {
printf("Enter two \n");
}
else {
printf("Invalid choice.\n");
}
return 0;
}
with sample output:
paul#thoth:~/src/sandbox$ ./cal
Enter 1 for Programmers Name and ID
Enter 2 to Perform Integer Operation
Enter 3 to Perform Floating Point Operation
2
Enter operator:*
Enter two integers :4 6
4 * 6 = 24
paul#thoth:~/src/sandbox$
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