Can't input value in SublimeText on MAC - c

This is my code :
#include <stdio.h>
int main()
{
int num;
printf("Enter the Number :");
scanf("%d",&num);
printf("\nNumber = %d",num);
return 0;
}
and when run this code I cant input the number , eventhough I didn't enter any number I get the output
Enter the Number :
Number = 1757380702[Finished in 0.1s]
What should I do for enter the input?
Thanks in Advance

I'm pretty sure Sublime Text just doesn't support that--see this related question. The only Mac text editor that I know for sure will do what you want is Chocolat.

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)

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.

Use isdigit in C programming

I would need some help regarding how to use isdigit() in this example:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int usr_option;
do
{
printf("\t\t\t************************MENU***********************\n");
printf("\t\t\t*** 1. Enter Code\t\t\t\t***\n");
printf("\t\t\t*** 2. Encrypt code and verify if correct\t***\n");
printf("\t\t\t*** 3. Decrypt code\t\t\t\t***\n");
printf("\t\t\t*** 4. Display number of times code was enter\t***\n");
printf("\t\t\t***\t\t(i) Successfully\t\t***\n");
printf("\t\t\t***\t\t(i) Unsuccessfully\t\t***\n");
printf("\t\t\t*** 5. Exit Program\t\t\t\t***\n");
printf("\t\t\t***************************************************\n");
printf("\nPlease enter your option from the menu: ");
scanf("%d", &usr_option);
if (isdigit(usr_option))
{
//Inside here is my switch case e.g switch(usr_option){/*code*/}
}
else
{
printf("Need to enter a digit\n");
}
}//end do while
while(usr_option != 5);
return 0;
}
How can I implement this code to not go into an infinite loop?
I've tried different ways but it seems like it doesn't want to work. I use the compilers CodeBlocks and Sublime Text 3.
If you have read a number via scanf("%d", &usr_option); Then you don't need to use is_digit() You can just use if or switch e.g.
switch(usr_option)
{
case 1: enter_code(); break;
case 2: encrypt(); break;
You only need is_digit if you are checking that characters in a string are digits or not.
i.e. '1' != 1

Simple Multichoice, Multivariable, Calculator Query

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.

Resources