Why can't I get the input value? - c

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.

Related

Nested if statement in C - why doesn't it evaluate the last else if?

The following code does not execute the last else if statement when you assign to choice value 3.
#include<stdio.h>
#include<stdlib.h>
int main() {
puts("Specify with a number what is that you want to do.");
puts("1. Restore wallet from seed.");
puts("2. Generate a view only wallet.");
puts("3. Get guidance on the usage from within monero-wallet-cli.");
unsigned char choice;
choice = getchar();
if ( choice == '1' ) {
system("nice -19 ~/monero-x86_64-linux-gnu-v0.17.2.0/monero-wallet-cli --testnet --restore-deterministic-wallet");
exit(0);
}
else if ( choice == '2' ) {
system("nice -19 ~/monero-x86_64-linux-gnu-v0.17.2.0/monero-wallet-cli --testnet --generate-from-view-key wallet-view-only");
exit(0);
}
else if ( choice == '3' ) {
puts("Specify with a number what is that you want to do.");
puts("1. Get guidance in my addresses and UTXOs");
puts("2. Pay");
puts("3. Get guidance on mining.");
unsigned char choicetwo = getchar();
if ( choicetwo == '1' ) {
printf("Use \033address all\033 to get all your addresses that have any balance, or that you have generated at this session.");
printf("Use \033balance\033 to get your balance");
printf("Use \033show_transfers\033 to get ");
printf("Use \033show_transfers\033 out to get ");
printf("Use \033show_transfers in\033 to get your balance");
}
}
return 0;
}
I get the following output When I enter 3:
Specify with a number what is that you want to do.
1. Restore wallet from seed.
2. Generate a view only wallet.
3. Get guidance on the usage from within monero-wallet-cli.
3
Specify with a number what is that you want to do.
1. Get guidance in my addresses and UTXOs
2. Pay
3. Get guidance on mining.
I'm really blocked, something is missing and I have no clue why it does not proceed to take the input from the user for the second time.
When you enter "3" for the first input, you're actually inputting two characters: the character '3' and a newline. The first getchar function reads "3" from the input stream, and the second one reads the newline.
After accepting the first input, you'll want to call getchar in a loop until you read a newline to clear the input buffer.
choice = getchar();
while (getchar() != '\n');

Why my c program only produce zero answer while using for loop?

I tried to use for loop calculate the number of books keyed in and sum up their total price, but at the end i only get zero price in C program. What is my problem ? How to solve it?
#include<stdio.h>
int main(void)
{
int booknum;
float bookfee,bookgst,nogst,totfee,newfee,newfee_nogst;
bookgst=0.0;
nogst=0.0;
int cnt;
char code;
printf("Key in total books purchased >> ");
scanf("%d",&booknum);
for(cnt=1;cnt<=booknum;cnt++)
{
printf("\n\n");
printf("Key in price of the book >> ");
scanf("%f",&bookfee);
printf("Key in type( S=standard gst,Z=zero gst) \n>> ");
scanf("%c",&code);
getchar();
if(code=='S')
{
newfee=bookfee+0.6;
}
else if(code=='Z')
{
newfee_nogst=bookfee;
}
bookgst=bookgst+newfee;
nogst=nogst+newfee_nogst;
printf("\n");
}
totfee=bookgst+nogst;
printf("Book purchased with GST : RM %.2f\n",bookgst);
printf("Book purchased without GST : RM %.2f\n",nogst);
printf("Total payment : RM %.2f\n",totfee);
return 0;
}
There are a few problems with this code, but you're almost there!
First code reading needs to eat the previous \n (see this), otherwise the code is neither Z not S (it's a newline), and that's why the fees are never added.
(Search also for "fgets vs scanf" to see how to use the safer fgets).
scanf(" %c",&code);
then these lines
bookgst=bookgst+newfee;
nogst=nogst+newfee_nogst;
add the newfee / newfee_nogst ; these variables are set to 0 before the loop, but at the next occurence, they're still set to the value of the previous occurrence, thus either set them to 0 at the beginning of the loop, or, add the value directly in the if (see below). And since we're here, print an error if the code is wrong (and maybe subtract one to cnt to do one more loop with a correct code, in this case).
Also, the GST calculation is probably wrong, 6% of x is 0.06 * x, and if you want GST added to the value that's x * 1.06
if(code=='S')
{
bookgst = bookgst + bookfee*1.06; // or bookgst += bookfee*1.06
}
else if(code=='Z')
{
nogst = nogst + bookfee; // or nogst += bookfee
}
else {
printf("Code not understood\n");
}

Printing only one combination using nested loops

I have to print combination of different currency notes a man should have to pay to the cashier. Program first ask total amount to be paid and then notes of different denominations that he possesses. The problem is, I have to print only one combination of currency notes, but I am getting all combinations (as usual) by the following code.
#include <stdio.h>
int main(void)
{
int hundred,fifty,ten,total,hund,fif,t;
printf ("Enter amount to be paid:");
scanf ("%d",&total);
printf ("\n\nHow many currency notes do you have?\nEnter 100,50 and 10 Rupee Notes Respectively:\n");
scanf ("%d %d %d",&hund,&fif,&t);
printf ("\t\t\tPossible combination for Rs=%d/- \n",total);
for (hundred=0; hundred<=hund; hundred++)
{
for (fifty=0; fifty<=fif; fifty++)
{
for (ten=0; ten<=t; ten++)
{
if ((hundred*100+fifty*50+ten*10)==total)
{
printf("\n\n Hundred rupees notes=%d, 50 rupees notes=%d, 10 rupees notes=%d",hundred,fifty,ten);
}
}
}
}
getch();
return 0;
}
Add a getch(); and return 0; just after the printf inside the nested loops.
Another way is to use goto. Type
goto exit;
Just after the printf inside the nested loops and type
exit:
Just before the getch();.

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.

Can't input value in SublimeText on MAC

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.

Resources