Thread: help with arrays and loops

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You did not finish off the main() function with a }, so you're attempting to define a function within a function, which is not allowed.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    apparently there is something wrong with my for loop; it compiles but after it asks for the hours for car 1 it asks for the rest of the cars but just assumes 0 and jumps to the end

  3. #18
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    any idea why?

  4. #19
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Looks like someone skipped hello world lesson.

  5. #20
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    once again, beginner here just asking for help. im the one who has written this code, its not like i had someone do it for me, so you can either fulfill the purpose of the thread and help or just not reply

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, what you did was bash away at your keyboard for a while without thinking.

    Then dumped the whole sorry mess on the board for someone else to fix.

    Except simply posting all the fixes won't teach YOU
    a) HOW to fix your own problems
    b) HOW to avoid making the same mistake again in future.

    We are guides to help you on your journey, not pack horses to give your lazy ass a free ride through life.

    If you can't deal with this, then just give up trying to be a programmer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #22
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    ha im not trying to be a programmer, its just a class required for my major. As i said earlier my program compiles but it doesnt actually loop through the for loop, it just goes through one time and after trying several times i posted on here that i was having trouble and i just get told that im not trying and you arent just going to do it for me

  8. #23
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    sorry if im getting agitated, ive been working on this thing for about a week now.

    this is my for loop

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float calculateCharges(float);
    const int NUMBER_OF_CARS= 8;
    
    main()
    {
     int cars[NUMBER_OF_CARS]= {1,2,3,4,5,6,7,8};
     float charges[NUMBER_OF_CARS];
     float hours= 0.0;
     float totalHours= 0.0;
     float totalCharges= 0.0;
     int i;
    
     for(i=0; i<NUMBER_OF_CARS; i++){
      printf("\nEnter the hours for car %d: ",cars[i]);
      scanf("%.1f",&hours);
      }
    it compiles, but my loop isnt working right. its asks you for the first input but then it doesnt ask for the next input the following times through the loop. so it just looks like this:

    Enter the hours for car 1: 6

    Enter the hours for car 2:
    Enter the hours for car 3:
    Enter the hours for car 4:
    Enter the hours for car 5:
    Enter the hours for car 6:
    Enter the hours for car 7:
    Enter the hours for car 8:

    can someone please help me with this

  9. #24
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Maybe reading this FAQ entry would help.

  10. #25
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    maybe im just an idiot but i didnt see how to implement that too my loop

  11. #26
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think you should really start listening to what people here are saying and drop this attitude of "I don't need this, I just want to get this done and over with so I never have to worry about it again". This DOES NOT WORK! You are not writing an essay on the American Civil War here.

    So, let me break it to you, unless you understand what you are doing you will not get this done. I promise you.

    Now, let's start with the basics again:

    1) main() should be
    int main(void)
    and should
    return 0;
    At the end of the function.

    2) Read the article you have been given from the FAQ AGAIN especially the 5th code snippet discussed there. That's of most interest to you now. Get the user's input correctly and post what you have after making these 2 changes.
    Last edited by claudiu; 11-26-2010 at 09:43 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #27
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    whenever i put the return 0 at the end of my function, it just makes the loop stop after one time through

  13. #28
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    i am an extreme beginner so i know you probably feel like youre trying to teach a brick wall but i promise i really am trying to do this on my own. its just all another language to me

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Of course it's frustrating - but follow the advise you're being given, and give it a good go.

    Let me see what's up with your code here...

    OK,
    1) You DO have to add a closing brace to main()
    2) there is no plain main() in C, only int main(void) or int main(argc, *argv[]).

    I know you like to discuss these things, but for now, just DO and let's move along here, as only doers can.

    3) Add the return 0; as the last line of code in main.

    4) Remove the "%.2f" from the scanf("%.2f", for the hours. My compiler won't even stop to scanf() that:
    Code:
    scanf("%f", &hours); //what you want
    You limit the amount of digits after the decimal point when you print, not when you take in the data. Not necessary, since 2.5 is perfectly fine for an entry, and if it becomes 2.500001 or whatever the nearest float approximation to 2.5 is, then that will be printed out properly with %.2f.
    Last edited by Adak; 11-26-2010 at 10:43 AM.

  15. #30
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    this is what my code is suppose to print out

    Enter the hours for car 1:
    Enter the hours for car 2:
    Enter the hours for car 3:
    Enter the hours for car 4:
    Enter the hours for car 5:
    Enter the hours for car 6:
    Enter the hours for car 7:
    Enter the hours for car 8:

    Cars Hours Charges
    1
    2
    3
    4
    5
    6
    7
    8
    TOTAL

    and then based on the inputs they will fill in the empty slots of the table

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays and loops
    By pmooney12 in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 10:38 PM
  2. Need help with for loops and arrays
    By Skeeter in forum C Programming
    Replies: 6
    Last Post: 10-23-2009, 03:33 PM
  3. Help with Arrays and For loops
    By Phil- in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2009, 10:34 AM
  4. Help using arrays and loops for math
    By LLINE in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2008, 04:09 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM