I wrote a program which takes three numbers from the user and prints out the maximum number, but when I run the program it is not taking the numbers from the user correctly
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1, num2, num3;
printf ("PROGRAM TO FIND THE BIGGEST NUMBER\n");
printf ("\n");
printf ("enter first number : ");
scanf ("%d ",&num1);
printf ("enter second number : ");
scanf ("%d ",&num2);
printf ("enter third number : ");
scanf ("%d ",&num3);
printf("%d - %d - %d \n",num1,num2,num3);
if (num1>num2 && num1>num3){
printf ("the biggest number is %d",num1);
}
else if (num2>num1 && num2>num3){
printf ("the biggest number is %d",num2);
}
else if (num3>num1 && num3>num2){
printf ("the biggest number is %d",num3);
}
return 0;
}
Remove the space after the %d in all the scanf calls. The space will match any character including the newline. The scanf will keep reading until the match fails. By removing the space the match fails when the newline character is entered and the code continues.
Related
Im a beginner to C and I'm having trouble with structs.
after I've asked user for all attributes, id like to print all the values of the struct. The problem is that after I've got input all the attributes to a struct and it loops back again to ask for attributes a second time, the first struct input gets replaced by the second struct input.
I'm pretty sure that I'm allocating the same memory space over and over and thus causing the problem but I'm stuck on how to fix it. Id appreciate any advice on what i can do. thanks!
case 2:
printf ("Please input a SKU number:");
scanf ("%d", &item[MAX_ITEMS].sku_);
printf ("Quantity:");
scanf ("%d", &item[MAX_ITEMS].quantity_);
printf ("Price:");
scanf ("%f", &item[MAX_ITEMS].price_);
printf ("The item is successfully added to the inventory");
break;
to print out the sku, quantity and price
switch (menuSelection) {
case 1:
printf ("Inventory\n");
printf ("=========================================\n");
printf ("Sku Price Quantity\n");
for (i =0 ; i<=MAX_ITEMS; i++){
printf ("%d %.2f %d\n", item[i].sku_, item[i].price_, item[i].quantity_);
}
printf ("=========================================\n");
break;
here is my whole code:
#include <stdio.h>
#define MAX_ITEMS 10
struct Item{
int sku_;
float price_;
int quantity_;
}item[MAX_ITEMS];
int main (void) {
int size=0;
int menuSelection;
int i=0;
printf ("Welcome to the Shop\n");
printf ("===================");
do {
printf ("\nPlease Select from the following options:\n");
printf ("1) Display the inventory.\n");
printf ("2) Add to shop.\n");
printf ("0) Exit.\n");
printf ("select:");
scanf ("%d", &menuSelection);
if (menuSelection <0 && menuSelection >2){
printf ("Invalid input, try again: Please select from the following options:");
}
else {
switch (menuSelection) {
case 1:
printf ("Inventory\n");
printf ("=========================================\n");
printf ("Sku Price Quantity\n");
for (i =0 ; i<=MAX_ITEMS; i++){
printf ("%d %.2f %d\n", item[i].sku_, item[i].price_, item[i].quantity_);
}
printf ("=========================================\n");
break;
case 2:
printf ("Please input a SKU number:");
scanf ("%d", &item[size].sku_);
printf ("Quantity:");
scanf ("%d", &item[size].quantity_);
printf ("Price:");
scanf ("%f", &item[size].price_);
printf ("The item is successfully added to the inventory");
break;
case 3:
break;
}
}
} while (menuSelection != 0);
return 0;
}
You create an array of Item objects with the length of MAX_ITEMS, currently being 10. That is, your objects have the indices of 0 to 9. Yet when asking the user for input, you always store the data at item[MAX_ITEMS] which is out of bounds of your array.
As a side note, when printing your array, you always print it whole, meaning also uninitialised items.
You have to store how many items are already "added to the shop" and use this number to determine the next array index where user input has to be stored. When printing you only have to iterate over the items already stored. Don't forget bounds checking, e.g. don't allow new user input when your shop is full.
The problem is that you always save the new values in the same place:
item[MAX_ITEMS].sku_
Instead, you should have a counter that show how many items are stored and save the new values in the place that is equal to the counter:
item[counter].sku_
and after every insertion you should increase the counter:
counter++;
So, your code should look like this:
int counter=0;
...
case 2:
printf ("Please input a SKU number:");
scanf ("%d", &item[counter].sku_);
printf ("Quantity:");
scanf ("%d", &item[counter].quantity_);
printf ("Price:");
scanf ("%f", &item[counter].price_);
printf ("The item is successfully added to the inventory");
counter++;
break;
I hope I was helpful
The item you defined is a array with size of MAX_ITEMS, so the problem of you is not about struct but array.
In most of computer programming language, indexing an array should use a offset start on zero. That is, MAX_ITEMS of item[MAX_ITEMS] is out of range for the array, you got a bug and do not find it. When you adding a item to your shop, you should code like this:
case 2:
if (last < MAX_ITEMS - 1)
{
printf ("Please input a SKU number:");
scanf ("%d", &item[last].sku_);
// ...
last ++; // on success
}
else
{
print("oops, shop is full.");
}
So I wrote this program using coderunner,
#include <stdio.h>
int main()
{
int num1, num2;
scanf("%d%d", &num1, &num2);
if (num1 > num2)
printf("The min is:%d\n ", num2);
else
printf("The min is:%d\n ", num1);
return 0;
}
The problem is that the program wont run. It keeps showing this and then it stops after a while:
Removing the scanf fixed the issue, I've tried other programs using scanf and it was fine. Any ideas?
How do you expect scanf() to interpret e.g. 123 or 1232 as two integers? Chances are all digits you enter are "eaten" by the first %d, and then scanf() waits for more for the second.
You must use some separation, or some non-numeric character between them:
scanf("%d/%d", &num1, &num2);
This tells scanf() to expect a slash between the two numbers. You could just use whitespace (without any in the format string, as pointed out in comments) too of course.
Also, you should check the return value before relying on the numbers:
if(scanf("%d %d", &num1, &num2) == 2)
{
}
I have written a program to take input of two numbers and either add or subtract the numbers depending the operation specified.
Here is my code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
float i, j, k;
char a;
printf("This is a program to add or subs two number.\n");
printf("Enter the first number : ");
scanf("%f", &i);
printf("Enter the second number : ");
scanf("%f", &j);
printf("Give your choice(+ or -): ");
scanf("%c", &a);
switch(a){
case '+' :
k = i + j;
printf("Sum = %f\n", k);
break;
case '-' :
k = i - j;
printf("Difference = %f\n", k);
break;
default:
printf("Cannot do this operation\n");
}
return 0;
}
This program takes input for the two numbers but skips input for operation and runs the default case. Please help!
(I am using gcc compiler).
The %c conversion specifier won't automatically skip any leading whitespace, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately.
One way around the problem is to put a blank space before the conversion specifier in the format string:
scanf(" %c", &a);
The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.
-Aditya
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a, b, checka, checkb;
printf ("enter a: ");
checka = scanf ("%d", &a);
printf ("enter b: ");
checkb = scanf ("%d", &b);
printf ("checka = %d\n", checka);
printf ("checkb = %d", checkb);
return EXIT_SUCCESS;
}
I was having this problem in a larger program but I wrote a quick test to see if I could fix it, which I can't.
Basically when anything other than an integer is entered for a scanf, the program just instantly skips and ignores every other scanf and just prints the rest of the program it sees, meaning I can't make checks with a while loop, or I just get an infinite loop as the scanf in the loop to fix the variable just gets skipped.
Obviously if integers are entered this particular program will just return 1 for the last two printfs, which is expected.
What am I doing wrong?
Thanks!
There is a reason why we should check scanf for errors, try this:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a, b, checka, checkb;
printf ("enter a: ");
if((scanf ("%d", &a)) == 1){
checka = a;
}else{
printf("Error(1)!");
exit(EXIT_FAILURE);
}
printf ("enter b: ");
if((scanf ("%d", &b)) == 1){
checkb = b;
}else{
printf("Error(2)!");
exit(EXIT_FAILURE);
}
printf ("checka = %d\n", checka);
printf ("checkb = %d", checkb);
return EXIT_SUCCESS;
}
For example when I input 2 for num1 and 3 for num2, I expect to get 8 for the output as soon as I enter the second number. However, the program expects me to input one more integer, and I just input a random number like 242 and it still outputs 8, which means that it does not affect the result. So my question is why is there the third input?
Thank you for your help!
#include "stdafx.h"
int Power (int num1, int num2);
int main ()
{
int a, b;
puts ("Enter two numbers, a and b:\n");
scanf ("%i\n", &a);
scanf ("%i\n", &b);
printf ("%i\n", Power(a, b));
return 0;
}
int Power (int num1, int num2)
{
int sum=1;
for (int i=1; i<=num2; i++){
sum= sum*num1;
}
return sum;
}
Get rid of the newlines: \n, in your scanf format strings, or just use a single scanf, e.g.:
scanf("%i%i", &a, &b);
Or:
scanf ("%i", &a);
scanf ("%i", &b);
Your scanf() doesn't need the "\n".
scanf ("%i", &a);
scanf ("%i", &b);
You should remove the '\n' from your format string in your calls to scanf.