Simple Multichoice, Multivariable, Calculator Query - c

I recently programmed this code (C) for a pretty simple calculator in Xcode.
It works mostly but does not display the answer for the sum/s.
The code is as follows:
#include <stdio.h>
int main()
{
//int's & chars-----------------
char SumMethod;
int firstnumber, secondnumber;
//int's & chars------------------
puts("Calculator v0.6");
printf("Please input first number: "); //Prompts 'firstnumber' input
scanf("%d", &firstnumber); //Scan's 'firstnumber' input and saves to '&firstnumber'
printf("Please input second number: "); //Prompts 'secondnumber' input
scanf("%d", &secondnumber); //Scan's 'secondnumber' input and saves to '&secondnumber
printf("d" "Please select Method: +(a), -(b), *(c), /(d): ");
scanf("%c", &SumMethod); //Scan's 'SumMethod' input
if(SumMethod=='a') {
printf ("%d",firstnumber + secondnumber); //This section detects the SumMethod and outputs the corrisponding sum
}
else if (SumMethod=='b'){
printf("%d",firstnumber-secondnumber);
}
else if (SumMethod=='c') {
printf("%d",firstnumber * secondnumber);
}
else if (SumMethod=='d') {
printf("%d",firstnumber / secondnumber);
}
}
For the solution i'm looking for a response that doesn't drastically change the code as how it is now is what I understand and because i'm relatively new to coding in general however if there is an obvious fix using useful common tools that would be much appreciated.
Thanks, Xenon

Your problem is that your comparisons are wrong. You need to compare your input to the LETTER a and not the (undefined and essentially random) values of the variables a,b,c,d.
so it is
if(SumMethod=='a')
instead of
if(SumMethod==a)
You don't need your variables a,b,c,d at all.

Related

How to fix 'Input/Output error' when attempting to make a simple calculator in C

New to C and am attempting to make a Calculator for a homework assignment. However, I am running into some problems
So far I have only attempting testing the add option but am still coming up with an Input/Output error when attempting to do so.
I am attempting to make a calculator that looks something like this:
Enter Number One:
Enter Number Two:
1-Add
2-Subtract
3-Multiply
4-Divide
5-Mod
Enter your choice:
Result=
int choice, result;
float x,y ;
int main() {
printf("Enter Number One:",&x);
scanf("%f",&x);
printf("Enter Number Two:",&y);
scanf("%f",&y);
printf("1-Add\n2-Subtract\n3-Multiply\n4-Divide\n5-Mod\n");
printf("Enter your choice:",&choice);
scanf("%d",&choice);
if (choice==1) {
result=x+y;
printf("Result=\n%f+%f=%f",x,y,result);
}
}
Enter Number One:1
Enter Number Two:2
1-Add
2-Subtract
3-Multiply
4-Divide
5-Mod
Enter your choice:1
Result=
0.000000+0.000000=0.000000read from master failed
: Input/output error
RUN FAILED (exit value 1, total time: 1s)

C LANGUAGE >NOOB HERE< what did i do wrong?

for(a=0;a<99;a++){
reg:
system("cls");
printf("\t\t\t~~REGISTER AN ACCOUNT~~\n\n");
printf("\tDesired Username: ");
scanf("%s", &user[a].user);
for(b=0;b<=a-1;b++){
if(strcmp(user[a].user,user[b].user)==0){
printf("USERNAME IS ALREADY TAKEN");
system("pause");
goto reg;
}
}
printf("\tDesired Password: ");
scanf("%s", &user[a].pass);
printf("\tPersonal/Company Name: ");
scanf("%s", &user[a].name);
printf("\tAddress/Location: ");
scanf("%s", &user[a].address);
printf("\tEmail-Address: ");
scanf("%s", &user[a].email);
printf("\tContact Number: 09");
scanf("%d", &user[a].contact);
break;
}
whenever I put the break in the end the system doesn't recognise if the username is taken or not, but when 'break' is removed it can recognise it.. idk how is this happening.. please help..
PS. sorry if this looks or sounds really stupid to you I am just starting to learn to program(I m trying to make a register program with a very limited knowledge for a school project)
I think the problem lies in your for loops:
First iteration:
a = 0;
for(b=0;b<=a-1;b++)
will not be true
as b<=a-1 equates to b<=-1 which is not true. Hence it will not enter the for loop for b. It will go to break and exit
Same process will repeat until a>=2,
When you don't have break , b waits for b>=2 and starts comparing. That's why you see result of comparison.
A simple check would be to print out values of a and b at every iteration.

Running C program returns -1.#QNAN0 instead of number stored in floating-point variable

Before going on, I'd like to say that this is my first time here and I don't know how things work yet so please pardon any errors on my part.
When compiled,(source code below) everything works fine except for the content of the float disp which is equal to -1.#QNAN0. Any help on this? Thanks in advance. Some parts of the code are not complete like the switch-case structure. Please temporarily that(Unless it affects the result).
The source code for the C program:
#include <stdio.h>
#include <stdlib.h>
float moneyup(float m);
int main()
{
char name[20];
char x;
int y;
float disp;
int hunger;
printf("\t\t**********************************************\n");
printf("\t\t* *\n");
printf("\t\t* How To Get Rich Quick! *\n");
printf("\t\t* *\n");
printf("\t\t**********************************************\n");
printf("\nThis is an experimental command line interface game made by NayNay AKA Nathan\n");
printf("\nPlease pardon the poor user interface.");
for(;;)
{
printf("\nPlease enter your name(one only)");
scanf("%s", &name);
printf("\nThe name you entered is %s. Is this correct? (type y/n for yes or no)\n");
fflush(stdin);
x=getchar();
if(x=='y') /*This part with the for loop is used to get the name of the*/
{ /*user and confirm the correctness of that name. If the name is*/
printf("Okay! Moving on..."); /*wrong, the user has the option to change it. Bulletproofing used*/
break; /*here*/
}
else if(x=='n')
{
printf("Alright let's try again.");
continue;
}
else
{
printf("Let's try this again.");
continue;
}
}
printf("\nOkay %s, Let's get this story started",name);
printf("\n\nOne sad dreary morning, %s got up from sleep and went to the kitchen to get breakfast.");
printf("\nUnfortunately for him his pantry only contained a bunch of cockroaches going at it and laying their eggs everywhere");
printf("\nHe then checked his pockets and pulled out his last 5-dollar bill. That was all he had left,");
printf("\nHe bought a sandwich for $2 and decides to start a business with $3 as capital");
printf("\n\nChoose how to start");
printf("\n1. Begging.");
printf("\n2. Mow lawns.");
printf("\n3. Apply for post of newspaper boy.");
fflush(stdin);
y=getchar();
switch(y)
{
case '1':
printf("You begged for 6 hours and got $5.25\n");
disp=moneyup(5.25);
printf("You now have $%f\n",disp);
}
return 0;
}
float moneyup(float m)
{
float money;
money=(float)money+m;
return(money);
}
The variable money is uninitialized in the function moneyup when used in expression
money=(float)money+m;

Why can't I get the input value?

I cannot assign the input value to bj.
For example,when I input 756,what I expect to get is UTC at the same time: 2356,however the computer gives me UTC at the same time: 1600.
#include <stdio.h>
int main()
{
int bj;
scanf("Input a BJT: %d", &bj);
if(bj>=0&&bj<=2359&&bj%100<60)
{
if(bj<800)
bj+=1600;
else
bj-=800;
printf("UTC at the same time: %d",bj);
}
else
;
return 0;
}
scanf("Input a BJT: %d", &bj);
You've mixed up your printf and scanf functions somewhat. scanf specifies what the input should be so you don't want the output stuff in there as well as it'll end up expecting that in your input.
Split them out like this.
printf("Input a BJT:\n");
scanf("%d", &bj);
scanf("Input a BJT: %d", &bj);
This line is messed up. Use it like:
printf("Input a BJT: ");
scanf("%d", &bj);
check this link
There are two solutions:
1.
printf("Input a BJT:\n");
scanf("%d", &bj);
2.
When you input, you should input Input a BJT: before you input the value of bj .For example,I want bj equal 756, so I input Input a BJT: 756.

program running through my if else after function call

I have a class assignment in C to make a simple calculator that performs three calculations. I haven't completed all of the functions yet but I am having a problem with my calcMenu function. When the function is called the program runs through all of the if else statements and unknown to me, performs only the else statement which is error checking. Than the function is run again which is intended but this time it does not run through all of the if else statements and allows the user to make a choice. I know I have done something really stupid but have been racking my brain for the last hour. If anyone has any pitty for me, than please point me in the right direction. I know all the system calls will Irk some but this is a basic class and our instructor has told us to use them.
Thanks in advance,
Mike
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define pause system ("pause")
#define cls system ("cls")
//Prototype calculate functions here
void evenOrOdd(int userNumber);
void squareNum(int userNumber);
void cubeNum(int userNumber);
void calcMenu(int userNumber);
void main() {
//Declare local variables here
int userNumber = 0;
printf("\t\t\tThe amazing three function caluculator\n\n\n");
printf("Please enter a whole number that you would like to calculate\n");
scanf("%d", &userNumber);
calcMenu(userNumber);
}
void calcMenu(int userNumber)
{
char calculateOption;
printf("\nWhat calculation would you like to perform with your number?\n\n");
printf("Press A.) to check if your number is even or odd.\n\n");
printf("Press B.) to calculate the square of your number.\n\n");
printf("Press C.) to calculate the cube of your number.\n\n");
printf("press D.) to exit the program.\n");
scanf("%c", &calculateOption);
calculateOption = toupper (calculateOption);
if (calculateOption == 'A')
{
evenOrOdd(userNumber);
}
else if (calculateOption == 'B')
{
squareNum(userNumber);
}
else if (calculateOption == 'C')
{
cubeNum(userNumber);
}
else if (calculateOption == 'D')
{
system("cls");
printf("Thank You for using my amazing calculator.\n\n");
system ("pause");
system ("exit");
}
else
{
printf("Please enter a valid choice");
calcMenu(userNumber);
}
}
void evenOrOdd(int userNumber) {
userNumber = userNumber %2;
if (userNumber == 0)
{
printf("Your number is even. \n");
}
else
{
printf("Your number is odd. \n");
}
}
void squareNum(int userNumber) {
}
void cubeNum(int userNumber){
}
When you read input with scanf you have to press the Enter key to make the program continue. Your scanf call reads the single character from the input, but leaves the Enter key still in the input buffer, to be read next time you call scanf.
There is a very simple trick to solve that: Place a space in the scanf format string before or after the "%c". This will make scanf skip whitespace.
scanf("%c ", &calculateOption);
If you stepped through the code with a debugger you would have easily seen that calculateOption would have been the newline character.
First of all, You can condense all those printf statements into one function to save the extra calls.
Next, you should probably indent your functions, I can't tell where one begins and another ends at a glance.
Third, don't use system("pause"), use getchar().
Fourth, this is optional, you might want to turn those if statements into a switch statement.
Now, on to your question. First of all, instead of using scanf("%c", &calculateOption), just use getchar() here too. In this case, I would write calcMenu() as this:
int calcMenu(int userNumber){
printf("\nWhat calculation would you like to perform with your number?\n\n\
Press A.) to check if your number is even or odd.\n\n\
Press B.) to calculate the square of your number.\n\n\
Press C.) to calculate the cube of your number.\n\n\
Press D.) to exit the program.\n");
switch(toupper(getchar())){
case 'A':
evenOrOdd(userNumber);
break;
case 'B':
squareNum(userNumber);
break;
case 'C':
cubeNum(userNumber);
break;
case 'D':
system("cls"); //this is bad, really.
printf("Thank You for using my amazing calculator.\n\n");
getchar();
return 0;
default:
printf("Please enter a valid choice: ");
calcMenu(userNumber);
break;
}
}
Also, main should always return a value. void main is bad practice.
Disclaimer: The code isn't tested, you shouldn't copy/paste it anyways. I also don't know if you're being forced to use some things or not...

Resources