Loop and if statement help

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 2322 Views - Last Post: 14 February 2015 - 08:54 AM Rate Topic: -----

#1 285matt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 25-January 15

Loop and if statement help

Posted 10 February 2015 - 08:10 AM

I have a assignment that needs to use if statements and loops, but I seem to have confused myself in the process of writing it as I thought i knew how to use loops for last weeks assignment. Heres the problem:
You have been asked to write a program to manage the shopping cart feature of your family’s online business. Your program should work as follows
1. ask the user for the cost of the item to be added to the cart
2. ask the user for the number of such items that should be added to the cart
3. update the new cart total
4. display a message indicating the new cart total and that the item/s have been added to the cart
5. It should ask the user if they want to check out or continue shopping
6. If the user selects to continue shopping, the program should resume from step 1.
7. When the user opts to checkout, the program should print out the current total for the cart,
thank the user for payment and say goodbye

Restrictions:
Please ensure that your program outputs the exact text as in the sample run. Naturally the numeric values will be different, depending on the input used. However the text of the output should be the same.
You may assume that the user always enters a valid float and integer for the price and the number of the item to be added. However, program should not allow the user to enter negative, or 0 values.
You may assume that the user always enters valid input when asked if to continue of checkout.

My probably terrible code:/
//
//  main.c
//  ShoppingCart
//
//  Created by Matt Lovell on 2/9/15.
//  Copyright (c) 2015 Matt Lovell. All rights reserved.
//

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int number, decision,Y,N;
    
    float price,total,new;
    
    
    
    // insert code here...
    
    printf("What is the price of the item that you want to add to cart\n");
    scanf("%f",&price);
        if(price<=0)
            printf("Please enter a price greater than $0.00");
        else
            
    printf("how many of the item do you want\n");
    scanf("%d",&number);
        if(number<=0)
            printf("Please enter a number of items greater than 0");
    printf("Your item has been added to the cart\n");
    
    total=price*number;
    
    printf("$%.2f has been added to the cart\n",total);
    printf("Your new cart total is $%.2f\n",new);
    printf("----------------------------------\n");
    printf("Would you like to continue shopping? Y for yes, N for no\n");
    scanf("%d", &decision);
        if (decision==Y)
            printf("What is the price of the item that you want to add to cart\n");}

    else
        printf("You have selected to checkout\nYour bill is $%.2f\nThanks for shopping with us\nGoodbye");

    return 0;
}



The problems I'm having is getting it to loop back to the beginning if the user enters Y. Also, its not running through the if statements properly and total isn't being printed. Am I totally off and doing this wrong or have a made a dumb mistake? I appreciate any help or constructive criticism from you guys. You've helped me understand my C problems in the past and its really helped me in my schooling.

Thanks,
-Matt

Is This A Good Question/Topic? 0
  • +

Replies To: Loop and if statement help

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Loop and if statement help

Posted 10 February 2015 - 08:57 AM

Quote

The problems I'm having is getting it to loop back to the beginning if the user enters Y.

Your assignment states that you should be using loops, so where's the loop. This part of the problem should be solved with a loop.

Quote

Also, its not running through the if statements properly and total isn't being printed.

I suggest you start adding braces to your control statements since you don't seem to understand that without the braces only a single line is contained within the control statement. As for the total not being printed, you're misusing the printf() statement, I suggest you check your documentation for this standard function.



Jim
Was This Post Helpful? 1
  • +
  • -

#3 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Loop and if statement help

Posted 10 February 2015 - 08:59 AM

Lines 22 & 24 have no parenthesis. This is dangerous when you are new as it makes assumptions. Plus you are indenting code (on line 22) & then it's un-indented again when it's in the loop. Though this wont cause errors it can cause confusion.

Then you have another else on line 42. What does this go to?

Your issue (& possibly your confusion) isn't with the logic, it's with syntax.
Was This Post Helpful? 1
  • +
  • -

#4 TheAlmightyPug   User is offline

  • New D.I.C Head

Reputation: 19
  • View blog
  • Posts: 47
  • Joined: 26-December 14

Re: Loop and if statement help

Posted 10 February 2015 - 09:13 AM

With regards to the loop not going back to the beginning, I would suggest using a while loops when you want a section of code repeated until a specific condition is met (i.e. keep prompting the user for a price until they enter a price greater than 0).
Was This Post Helpful? 2
  • +
  • -

#5 285matt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 25-January 15

Re: Loop and if statement help

Posted 10 February 2015 - 09:17 AM

So should I have have a while(Y) start before line 20 and have a bracket after line 40? The else in line 42 should be when the user says N, so the program prints a few lines and ends.Line 24 i want it to just continue on with the rest of the code. I had a while(Y) but deleted it because my code wouldnt even run. Thanks for the quick replies!
-Matt
Was This Post Helpful? 0
  • +
  • -

#6 TheAlmightyPug   User is offline

  • New D.I.C Head

Reputation: 19
  • View blog
  • Posts: 47
  • Joined: 26-December 14

Re: Loop and if statement help

Posted 10 February 2015 - 09:22 AM

printf("What is the price of the item that you want to add to cart\n");
scanf("%f",&price);

while(price<=0)
{
    printf("Please enter a price greater than $0.00");
}



For example, the above loop will keep prompting the user until they enter a price greater than 0.
Was This Post Helpful? 1
  • +
  • -

#7 285matt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 25-January 15

Re: Loop and if statement help

Posted 10 February 2015 - 09:24 AM

Ohhhh. So instead of the if statements i should just use a while loop?
Was This Post Helpful? 0
  • +
  • -

#8 TheAlmightyPug   User is offline

  • New D.I.C Head

Reputation: 19
  • View blog
  • Posts: 47
  • Joined: 26-December 14

Re: Loop and if statement help

Posted 10 February 2015 - 09:26 AM

printf("What is the price of the item that you want to add to cart\n");
scanf("%f",&price);

while(price<=0)
{
    printf("Please enter a price greater than $0.00");
    scanf("%f",&price);
}



My apologies, I forgot to put the scanf() inside of the loop also.

Yes, based on what I can see you should be using while() loops in all of the places you are using if statements.
Was This Post Helpful? 1
  • +
  • -

#9 285matt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 25-January 15

Re: Loop and if statement help

Posted 10 February 2015 - 09:29 AM

Alright. I'll give it a shot after class. Thanks for the help Pug! Will this then fix my total from not outputting? I had it working earlier before I added all the if statements so I assume I messed it up in the process. Thanks for the help everyone!
-Matt
Was This Post Helpful? 0
  • +
  • -

#10 TheAlmightyPug   User is offline

  • New D.I.C Head

Reputation: 19
  • View blog
  • Posts: 47
  • Joined: 26-December 14

Re: Loop and if statement help

Posted 10 February 2015 - 09:40 AM

Jim addressed why the final printf() statement isn't working correctly. I would suggest you look at it and try to figure out what might be wrong. Maybe look at the documentation for printf() or compare it to the other printf() functions in your code.
Was This Post Helpful? 1
  • +
  • -

#11 285matt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 25-January 15

Re: Loop and if statement help

Posted 10 February 2015 - 07:47 PM

Well i did some work on it. How do i get it to loop back to the beginning if the user inputs a Y? I know i have 2 if statements that don't do anything at the end, and a while loop in the beginning that doesnt do anything, i just put them there because thats where I thought i needed them to go. However I'm most likely wrong. Thanks again for the help everyone!
//
//  main.c
//  ShoppingCart
//
//  Created by Matt Lovell on 2/9/15.
//  Copyright (c) 2015 Matt Lovell. All rights reserved.
//

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int number, decision,Y,N;
    
    float price,total = 0.0,new;
    
    
    
    // insert code here...
    while (decision==Y)
    printf("What is the price of the item that you want to add to cart\n");
    scanf("%f",&price);
    while(price <=0){
        printf("Please enter a price greater than $0.00\n");
        scanf("%f",&price);}
    printf("how many of the item do you want\n");
    scanf("%d",&number);
        if(number<=0)
            printf("Please enter a number of items greater than 0");
    printf("Your item has been added to the cart\n");
    
    total=price*number;
        
    printf("$%.2f has been added to the cart\n",total);
    new=new+total;
    printf("Your new cart total is $%.2f\n",new);
    printf("----------------------------------\n");
    printf("Would you like to continue shopping? Y for yes, N for no\n");
    scanf("%d", &decision);
    if (decision==Y)
        
    if (decision==N)

    printf("You have selected to checkout\nYour bill is $%.2f\nThanks for shopping with us\nGoodbye",total);

    return 0;
    }


Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Loop and if statement help

Posted 10 February 2015 - 07:53 PM

You'll want to use a do-while loop around the block of code that you want repeated.
Was This Post Helpful? 1
  • +
  • -

#13 285matt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 25-January 15

Re: Loop and if statement help

Posted 10 February 2015 - 08:02 PM

I change line 19 to
do{
and line 39 to
}while(decision==Y
. But it just infinitely runs. Did I place the parenthese wrong?

Well that turned out bad.... line 39 is
}while(decision==Y);
But it still just keeps running through as if it ignores my input.
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Loop and if statement help

Posted 10 February 2015 - 08:07 PM

How does an integer equal Y?

On line 12 you declare some integer variables, & don't assign them values. You then read input into variable decision & loop while it's not equal to the variable Y.

How I troubleshoot items like this, is I print out the variable value regardless of what I think it is, so I can verify what is happening.
Was This Post Helpful? 0
  • +
  • -

#15 285matt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 25-January 15

Re: Loop and if statement help

Posted 10 February 2015 - 08:20 PM

So like this?
int number;
    char decision;
    
    float price,total,new;
    
    
    
    // insert code here...
    do{
    printf("What is the price of the item that you want to add to cart\n");
    scanf("%f",&price);
    while(price <=0){
        printf("Please enter a price greater than $0.00\n");
        scanf("%f",&price);}
    printf("how many of the item do you want\n");
    scanf("%d",&number);
        if(number<=0)
            printf("Please enter a number of items greater than 0");
    printf("Your item has been added to the cart\n");
    
    total=price*number;
        
    printf("$%.2f has been added to the cart\n",total);
    new=new+total;
    printf("Your new cart total is $%.2f\n",new);
    printf("----------------------------------\n");
    printf("Would you like to continue shopping? Y for yes, N for no\n");
    scanf(" %c", &decision);
    if (decision=="y")

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3