Thread: Help In Understanding Error

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Help In Understanding Error

    First, let me start by saying this is not a homework or school assignment. I am attempting to learn C on my own, by myself, and have purchased some books on the subject, including C- How to Program 8th Ed. and C Programming Absolute Beginners Guide. I have also watched through the first 15 videos by "thenewboston" on youtube to get a further understanding of what I am reading. So far, I am comfortable with headers, operators, variables, scanf, and printf functions and was attempting to put all the learning together to come up with a simple bills payment calculation. My compiler, (CodeBlocks), doesn't throw any compile errors but upon execution the program has a fatal error after the last scanf it seems. I have looked at things over and over and searched through my books but I can't detect the problem. I have tried using integers instead of floats and that didn't correct either. Any assistance would be greatly appreciated. It could be something very simple but I deleted all code and tried again in an attempt to minimize any small rookie errors but still can't get it to run to completion. Thanks!


    Code Below:

    Code:
     #include <stdio.h>
     #include <stdlib.h>
    
    
     int main()
     {
     float paycheck, bill1, bill2, savings, spending;
     char bill_1[20];
     char bill_2[20];
     printf("How much did you get paid this week?\n");
     scanf("%f", &paycheck);
     printf("What is your first bill to pay this week?\n");
     scanf("%s", &bill_1);
     printf("How much is that bill in dollars?\n");
     scanf("%f", &bill1);
     printf("What is your second bill to pay this week?\n");
     scanf("%s", &bill_2);
     printf("How much is that bill in dollars?\n");
     scanf("%f", bill2);
     savings = (paycheck - (bill1 + bill2)) * .15;
     spending = paycheck - (bill1 + bill2) - savings;
     printf("You will pay a total of %.2f in bills, paying %s and %s, save %.2f dollars, and then have %.2f dollars to spend.", bill1+bill2, bill_1, bill_2, savings, spending);
    
    
     return 0;
     }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    "bill_1" and "bill_2" are pointers by themselves, you don't need an ambersand for them. On the other hand, "bill2" does need it.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    2
    lol, missed an ambersand. That was silly. Will amend that. So that corrected it. Why are bill_1 and bill_2 pointers by themselves? Just because the variable is of the string type?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by trey.nobles View Post
    Just because the variable is of the string type?
    Any static array has that feature. Its name is a pointer to its first element. This was implemented in order to maintain consistency, since a pointer can be used as an array, it logically follows that an array can be used as a pointer.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Some Understanding
    By Typina in forum C Programming
    Replies: 3
    Last Post: 03-29-2010, 01:30 PM
  2. Help understanding error message
    By kotoko in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 11:57 AM
  3. Need help understanding..
    By TylerMoyer in forum Game Programming
    Replies: 2
    Last Post: 08-07-2007, 10:11 AM
  4. Help understanding .NET
    By subnet_rx in forum Windows Programming
    Replies: 7
    Last Post: 09-02-2004, 03:27 PM
  5. new question: understanding parse error
    By Pig in forum C++ Programming
    Replies: 3
    Last Post: 11-19-2003, 01:14 PM

Tags for this Thread