Thread: total newbie

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    8

    total newbie

    hii.

    Im afraid im a total newbie to programming and have a code to complete for an assignment but im stuck. the code is meant to pipe in data from a USB using cmd prompt which i have completed.

    what i want is to happen is that insted of reading out line 1 and line 2 then line 3 and line 4 then line 5 and line 6 and so on. i want it to read in line 1 and line 2 then line 3 and line 2 again making line 2 dprev and pprev insted of d and p for the second calculation.


    where am i going wrong????

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    
    
    {
        float p, pprev, e;
        int   d, dprev ;
    
    
    
    
        while (feof(stdin)==0)
    
    
           {
    
    
            scanf("%d %f", &dprev, &pprev );
            printf(" dprev = %d, pprev = %f\n", dprev,pprev);
    
    
    
    
            scanf("%d %f", &d, &p );
            printf("d = %d,p = %f\n", d, p);
    
    
            e = 100 * pprev / (d - dprev);
    
    
            printf("The efficiency is %f\n", e);
    
    
    
    
            dprev = d;
            pprev = p;
    
    
          }
            return 0;
        }
    my data is
    54752 2.64
    53247 2.87
    52247 2.20
    57412 2.78
    59321 2.58
    49665 2.86
    52001 2.55

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    Or does this even make since :P

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, if you don't want it to read in values twice in each loop, then don't put two read statements (i.e. scanf calls) in the loop.

    Read this: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Also, you should check the return value of scanf. It returns the number of successfully scanned items. You ask for two things (an int and a float), so if it doesn't return 2, you know there was an input error, and you should probably stop the program. Better yet, make the scanf call your loop condition:
    Code:
    while (scanf(...) == 2) {
    If it's not 2, you had an input error and you can exit.

    You might want to initialize dprev and pprev with values that make sense for your first calculation (0, 1, 42?), or have a single scanf call before the loop to pre-populate them. I'll leave figuring that out up to you since you know what the formula should do.

    Lastly, you should add a temporary variable that contains the value of d - dprev. Check that's it's not 0 before you try to divide, since dividing by zero is not allowed.
    Last edited by anduril462; 10-02-2014 at 11:39 AM. Reason: fix wording

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    Thanks for the reply.

    I do want it to read in the values from the two lines in one loop. i want it to read d prev as 54752 and pprev as 2.64 and d 53247 as and p as 2.87 in the first loop and then 53247 will be dprev and 2.87 will be pprev and i will take 52247 and 2.20 as d and p in the second loop if that makes since

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There are a few ways you can go about this.

    One would be to, before the loop, read the initial values into "dprev" and "pprev". Then you enter the loop, reading into "d" and "p", do your calculations (or whatever), and then move "d" into "dprev" and "p" into "pprev", and repeat.

    Is that along the lines of what you were thinking?

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    Quote Originally Posted by Matticus View Post
    There are a few ways you can go about this.

    One would be to, before the loop, read the initial values into "dprev" and "pprev". Then you enter the loop, reading into "d" and "p", do your calculations (or whatever), and then move "d" into "dprev" and "p" into "pprev", and repeat.

    Is that along the lines of what you were thinking?

    Yea exactly.

    I want to use the previous d and p as the dprev and prev in the next loop

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by stephen8ofclubs View Post
    I do want it to read in the values from the two lines in one loop. i want it to read d prev as 54752 and pprev as 2.64 and d 53247 as and p as 2.87 in the first loop and then 53247 will be dprev and 2.87 will be pprev and i will take 52247 and 2.20 as d and p in the second loop if that makes since
    Yes, it makes sense. And no, you don't want to actually read two sets of values in each loop. You want to read one set of values. That is, each time through the loop, you want d and p (and dprev and pprev) to "move down one line" in your input. Right? Thus you want to read one line (i.e. read once) per loop.

    So read once before the loop into dprev and pprev, then each time through the loop, read once. The last lines of the loop should update dprev and pprev to contain d and p respectively, which you already do. Use the while (scanf) construct I provided in post #2. Make sure your scanf call before the loop also checks for a return value of 2, and exits with an error if it doesn't get that.
    Last edited by anduril462; 10-02-2014 at 12:19 PM. Reason: wording

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by stephen8ofclubs View Post
    Yea exactly.

    I want to use the previous d and p as the dprev and prev in the next loop
    [Apologies to anduril462 - I just realized my suggestion simply echoed his.]

    So you've been given an approach to consider - have you tried coding it yet?

  9. #9
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    thanks for that

    so should my while loop look like
    Code:
     while ( scanf("%d %f", &dprev, &pprev == 2 ))
    ???

  10. #10
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Quote Originally Posted by stephen8ofclubs View Post
    thanks for that

    so should my while loop look like
    Code:
     while ( scanf("%d %f", &dprev, &pprev == 2 ))
    ???
    Um no, as
    Code:
    while ( scanf("%d %f", &dprev, &pprev) == 2 )

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You have a parenthesis in the wrong place (compare your code with the sample given in post #3).

  12. #12
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    
    
    {
        float p, pprev, e;
        int   d, dprev ;
    
    
        scanf("%d %f", &dprev, &pprev );
        printf(" dprev = %d, pprev = %f\n", dprev,pprev);
    
    
    
    
    
    
    
    
    
    
    
    
             while ( scanf("%d %f", &dprev, &pprev) == 2 )
        {
    
    
            scanf("%d %f", &d, &p );
            printf("d = %d,p = %f\n", d, p);
    
    
            e = 100 * pprev / (d - dprev);
    
    
            printf("The efficiency is %f\n", e);
    
    
    
    
            pprev = p;
            dprev = d;
    
    
        }
    
    
    
    
    
    
    
    
        return 0;
    }
    my code now looks like this but when i run it it skips a every second line of the txt. file and so is incorrect calculations ........

  13. #13
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
     
    int main()
     
     
    {
        float p, pprev, e;
        int   d, dprev ;
     
     
        scanf("%d %f", &dprev, &pprev );
        printf(" dprev = %d, pprev = %f\n", dprev,pprev);
     
     
     
     
     
     
     
     
     
     
     
     
             while ( scanf("%d %f", &dprev, &pprev) == 2 )
        {
     
     
            scanf("%d %f", &d, &p );
      printf("dprev=%d,pprev=%f\n",dprev,prev); /* this is the important line, you were not printing dprev and pprev */
            printf("d = %d,p = %f\n", d, p);
     
     
            e = 100 * pprev / (d - dprev);
     
     
            printf("The efficiency is %f\n", e);
     
     
     
     
            pprev = p;
            dprev = d;
     
     
        }
      
     
     
        return 0;
    }
    Look at the line I added, you were not printing dprev and pprev.

  14. #14
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    and if everything else was same the efficiency value should be correctly calculated even without that printf.

    If the data isnt being printed it doesent necessarily mean it isnt being used in the computation.

  15. #15
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    thanks guys finally think i have i correct.


    couldn't have done it without you all.


    HAVE A GOOD ONE!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Total Newbie (FNG)....
    By Bustsofa69 in forum C Programming
    Replies: 9
    Last Post: 03-07-2014, 11:05 PM
  2. Total newbie question
    By 20,000leeks in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2006, 07:58 PM
  3. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  4. Total newbie to programming C! Need help...
    By majortony in forum C Programming
    Replies: 3
    Last Post: 11-17-2005, 07:51 AM
  5. Total Newbie Here
    By Wiser in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-14-2003, 10:31 PM