Help a newbie

This is a discussion on Help a newbie within the C Programming forums, part of the General Programming Boards category; Ok, I am very new to c programming and have a little problem. I am currently using the pico editor ...

  1. #1
    Unregistered
    Guest

    Help a newbie

    Ok, I am very new to c programming and have a little problem. I am currently using the pico editor in UNIX and here is my problem:
    I have to calculate total revenue through a daily record of sales that looks like this:

    100 soap 15 0.98
    101 tape 25 0.35
    102 cds 5 13.95
    103 disks 8 1.50
    104 books 4 8.95
    105 mice 25 7.75
    106 pens 40 3.95
    107 erasers 12 0.92
    108 staples 20 0.38
    109 paper 9 3.92
    110 candy 18 1.50
    111 gum 40 0.95
    112 soda 25 0.75
    113 pretzels 40 1.95
    114 pencils 100 0.25

    First bumber is transaction number, second set of characters is the item, third is number of items sold, and last one is price. This data is stored in a text file which I must reassign the standard input to when I run the program to get the numbers. In my program, how do I isolate only the number of items sold and the price of each item so i can find the total daily transactions? Don't need entire code, just some suggestions on how to do this. Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd suggest starting with:

    int main( void )
    {

    And build up from there. You may even want to go one step before that, and throw in a few #include lines...

    How are you reading your data? Are you reading it using fgets? fread? fscanf? What exactly? With fscanf you can safely just extract the single (or multiple) value from the line that you want and ignore the rest. Combine fgets and sscanf, and you can do the same thing.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Code:
    FILE *fptr;
    
    // Open the file and do error check
    // ...
    
    fscanf(fptr, "%d %s %d %f", &transNum, &string, &numSold, &price);
    
    // Do any of your processing
    // ...
    Now you just put that in a loop and build around that. Good luck

  4. #4
    Unregistered
    Guest
    thanks for the help guys, but unfortunately I am only in my first semester of programming and we have yet to cover the methods that you guys suggest. I am unfortunately limited to somehow using only scanf. And other ideas?

  5. #5
    Unregistered
    Guest
    wait a second, I figured it out!! Thanks a lot guys, you helped a lot actually! I think i'm gonna be posting here more often, time to register! Thanks again!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I figured it out!!
    That's the best way

    >I think i'm gonna be posting here more often
    Joy. I suggest you read the links in my signature before posting again though.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21