Why does my program not allow me a second input? [duplicate] - c

This question already has answers here:
Why you mustn't write scanf("%.2lf", &a) in C?
(2 answers)
Closed 4 years ago.
Suppose savings and expenses are variables of type double that have been given values. Write an if-else statement that outputs the word Solvent, decreases the value of Savings by the value of expenses and sets the value of expenses to 0, provided that savings is more than expenses. If however, savings is less than expenses, the if-else statement simply outputs the word Bankrupt and does not change the value of any variable.
Specific question: Why does my program skip immediately through after I type in a value for savings? How can I remedy it?
#include <stdio.h>
void solvent();
int main(void)
{
double savings, expenses;
printf("\nEnter a number for savings: ");
scanf("%2lf", &savings);
printf("Enter a number for expenses: ");
scanf("%2lf", &expenses);
if(savings > expenses)
solvent();
else
printf("Bankrupt!");
system("pause");
return 0;
}
void solvent(double savings, double expenses)
{
printf("Solvent!");
savings -= expenses;
expenses = 0;
}

Okay, for reasons I don't quite understand, making %2lf be just %lf was sufficient to fix this. Thanks to Weather Vane.

Related

Counting how many dozen in a number and also count its extra amount? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
How do i make this program counts how many dozen in a number and also count its extra amount?
This is what only I came up
#include<stdio.h>
int main()
{
float number, dozen;
printf("Please Enter any integer Value : ");
scanf("%f", &number);
dozen = number / 12;
printf("dozen of a given number %.2f is = %.2f", number, dozen);
return 0;
}
I dont know how i will get to count the dozen in a number, for example there is 45, i need to get 3 dozen and the extra will be 9.
You prompt for an integer but then use floats. You already had the correct dozen calculation and just miss the modulo operator %. Reformatted code for readability.
#include <stdio.h>
int main() {
printf("Please Enter any integer Value : ");
int number;
scanf("%d", &number);
printf("dozen of a given number %d is %d with remainder %d\n",
number,
number / 12,
number % 12
);
return 0;
}
and example execution:
Please Enter any integer Value : 14
dozen of a given number 14 is 1 with remainder 2
Now that there is an accepted answer,
here's a small lesson in arithmetic (plagiarising #Allan Wind's answer):
#include <stdio.h>
int main() {
printf("Please Enter any integer Value : ");
int number;
scanf("%d", &number);
printf("dozen of a given number %d is %d with remainder %d\n",
number,
number / 12,
number - ( number / 12 * 12)
);
return 0;
}

Calling printf() after scanf() returns nothing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to learn C and I've started by building a simple change calculator. At the moment, I'm just trying to take the user input 8.68 and output it, output the loonies and output the quarters. However, the last 3 printf() calls return nothing:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void){
double value = 0;
int loonies = value;
double remainder = value - loonies;
printf("Please enter the amount to be paid: $");
scanf("%lf", value);
printf("%lf", value);
printf("%d", loonies);
printf("%lf", remainder);
}
This is the output. The prompt asks for the user input and then the program ends:
Please enter the amount to be paid: $8.68
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
You are missing an '&'. Scanf requires you to pass pointers to the variables you want to read into.
scanf("%lf", &value);
The scanf expects the address of the variable that you are passing. Here in your case you have to give &value.
The full code I am pasting here
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
double value = 0;
int loonies = value;
double remainder = value - loonies;
printf("Please enter the amount to be paid: $");
scanf("%lf", &value);
printf("Values- %0.2lf\n", value); //'\n' and 'Values-' added
printf("Loonies - %d\n", loonies); //'\n' and 'Loonies-' added
printf("remainder - %0.2lf\n", remainder); //'\n' and 'remainder-' added
}
Output I am getting is
Please enter the amount to be paid: $8.68
Values- 8.68
Loonies - 0
remainder - 0.00
Edit: Here I am giving %0.2lf for printing only two decimal characters
You are missing a & in your scanf.

how to assign spaced input numbers in array of unkown length [duplicate]

This question already has answers here:
Scan multiple integers without knowing the actual number of integers
(2 answers)
Closed 7 years ago.
Currently, I am asking user to specify number of input values being specified.
This the code for it:
#include<stdio.h>
#include<math.h>
#include<string.h>
void main()
{
int i,n;
printf("\nHow many record you will enter: ");
scanf("%d",&n);
float x[n];
printf("\n\nEnter the values of velocity (m/s):");
for(i=0; i<n; i++)
{
scanf("%f",&x[i]);
printf("\n%f",x[i]);
}
}
The code runs fine. But, I want to write code in such a way that it will calculate 'n' by scanning the input (numbers separated by space, not necessary one space between each number) without asking the user.
Can you suggest me a way for it.
PS: I am new to coding
Thanks in advance
You can have a look at fgets() and strtok(). Combining both of them , you can design as per your target. Also, you may need to know and use malloc() and free() to make use of dynamic memory allocation.
Maybe this answer can help you.

while loop does not work if nothing changes to terminate it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I can't quite figure out this while loop. i understand the concept i just don't know what to increase it by. Also, for some reason my running total doesn't work?
the idea behind this is setting a goal of money to save up in a jar. Every time i put a certain amount of money in the jar, i want it to give me the total amount of money in the jar, and also tell me how much more money i need to put in the jar to reach my goal.
here is my code:
#include <stdio.h>
int main() {
int goal;
int total = 0;
int deposite;
int ammountNeeded;
printf("How much money would you like to save?\n ");
scanf("%i", &goal);
printf("How much money are you putting in the jar?\n");
scanf("i%", &deposite);
total = total + deposite;
ammountNeeded = goal - deposite;
while (goal > total) {
printf("How much money are you putting in the jar?\n ");
scanf("i%", &deposite);
printf("You have saved R%i. ", total);
printf("You need to save another R%i in order to reach your goal.\n ",ammountNeeded);
}
if (total >= goal) {
printf("Well done! you have sucsessfully saved R%i", goal);
}
return 0;
}
The while loop has a condition. It executes the body as long as the condition is true. In your case, the condition does not change, because none of the 2 variables compared change its value inside the loop. You should either increase total or decrease goal.
This should fix it:
while (goal > total) {
printf("How much money are you putting in the jar?\n ");
scanf("i%", &deposite);
total = total + deposite; //Add this
printf("You have saved R%i. ", total);
ammountNeeded = goal - total; // And perhaps add this
printf("You need to save another R%i in order to reach your goal.\n ",ammountNeeded);
}

C: Need help extracting values from strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get a duration of time into minutes from a string. I am given a string like this: "1:50". And I need to extract the minutes and seconds from this strings into int variables and then return the duration in minutes. So I wrote this:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
int main()
{
char time[6]="01:30";
int duration=0, minutes=0, seconds=0;
int buffermin[3];
int buffersec[3];
int i=0;
while(i<2)
{
sscanf(time[i],"%d%d",&buffermin[i]); //Get the first two characters in the string and store them in a intger array
i++;
}
i=3;
while(i<5)
{
sscanf(time[i],"%d%d",&buffersec[i]); //Get the last two characters in the string and store them in a integer array
i++;
}
printf("%d %d %d %d", buffermin[0], buffermin[1], buffersec[0], buffersec[1]);
getch();
minutes=(buffermin[0]*10)+buffermin[1]; //Put values in array to one variable
seconds=(buffersec[0]*10)+buffersec[1]; //Same as above
seconds=seconds/60; //Turn the number of seconds to minutes
duration=seconds+minutes; //Get total duration
printf("The total duration is: %d\n",duration); //Output total duration
getch();
exit(0);
}
Why is this not working and how could I fix this. Any examples would be really very appreciated. If you have the time to explain how the example works, please do so. Still poor at programming as you can see.
You should really learn how to use sscanf properly. Basically, what you want to achieve is this:
#include <stdio.h>
int main() {
char time[] = "01:27";
int minutes;
int seconds;
// Must be double, not integer, otherwise decimal digits will be truncated
double duration;
// String has format "integer:integer"
int parsed = sscanf(time, "%d:%d", &minutes, &seconds);
// Check if input was valid
if (parsed < 2) {
// String had wrong format, less than 2 integers parsed
printf("Error: bad time format");
return 1;
}
// Convert to minutes (mind the floating point division)
duration = (seconds / 60.0) + minutes;
printf("Duration: %.2f minutes\n", duration);
return 0;
}

Resources