0

I am running in to an error which doesn't let me insert all of the required data. I am sure I have my for loops set up correctly. I'm not exactly sure on what I'm missing but if someone can over look my code, I would highly appreciate it!

I am using Dev C++.

#include <stdio.h>




    float loadArray(float *) ; 

    float calcTotal(float *); // calc total sales

    int main()
    {
   //declare variables

   float sales[7];
   float weeklyS;
   float total;
float totalS;
float highest;

//since these are pointers in the func, u need an &
sales[7] = loadArray(&weeklyS);

total = calcTotal(&totalS);     // when calling a pointer use a & to send the address
printf("Total of the week is: %.2f",totalS);

//calcHighest(odom,odom2,milesDrvn,avgMPG);


return 0 ;  

}



float loadArray(float *weeklyS)   
{
float sales;
int x;

for ( x = 0; x < 7; x++)
{

// have user enter the following
printf("Enter sales for day %d: ",x  + 1);
scanf("%d",&*weeklyS);

}

return sales;
}


float calcTotal(float *totalS)
{

float total = 0 ; 
int x;

for ( x = 0; x < 7; x++)
{
    total +=  *totalS;
    *totalS++;

}




return total;  /// return the accumulator
}

ive stated some comments in the code to explain what does what and am not too sure why this code is not running the way it should. I have declared all variables as floats, i believe i have also addressed my pointers correctly and called them properly as well with the * and the &.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
rids64
  • 19
  • 4
  • 3
    `scanf("%d",&*weeklyS);` but your variable is a `float *`. Why &* BTW? so `scanf("%f",weeklyS);` will do. Your compiler should warn you about this format mismatch. – Jean-François Fabre Nov 05 '17 at 20:34
  • 2
    Please format your code, it's unreadable. – Iharob Al Asimi Nov 05 '17 at 20:35
  • i was told to use that. Hate to be a bother, but do you mind explaining why its not needed?? thank you – rids64 Nov 05 '17 at 20:36
  • 3
    I suggest you [get a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to read first. – Some programmer dude Nov 05 '17 at 20:36
  • your code achieves nothing: scanning the same value in a loop doesn't make sense. – Jean-François Fabre Nov 05 '17 at 20:37
  • 2
    `sales[7] = ...` undefined behaviour array out of bounds. Don't you read the warnings. I think this could go on and on... – Jean-François Fabre Nov 05 '17 at 20:37
  • 2
    And Using uninitialized variable. – BLUEPIXY Nov 05 '17 at 20:39
  • @Jean-FrançoisFabre thanks for the feedback! i know my code obviously isnt the best and after nights of looking things up i was able to put this program together. I was really proud of myself that i knew this much and learned alot to get that code running. as i stated earlier, im still a rookie, trying to get better at coding. If you dont mind helping me by pointing out some more errors. I really appreciate it! – rids64 Nov 05 '17 at 20:42
  • there are too many errors to point out, but the good news is: enable all warnings and the compiler will point out the exact same errors that everyone reported to you. That'll be a good start. Good luck. – Jean-François Fabre Nov 05 '17 at 20:43
  • @Jean-FrançoisFabre i did that and this is what i got: – rids64 Nov 05 '17 at 20:47
  • Compilation results... -------- - Errors: 0 - Warnings: 0 - Output Filename: ************* hidden - Output Size: 128.666015625 KiB - Compilation Time: 1.58s – rids64 Nov 05 '17 at 20:47
  • either your compiler is crap or you did not enable all warnings. Check the options. If you cut off the warnings, you'll get 0 warnings. Also switch to clang or gcc, which have excellent warning diagnostics. – Jean-François Fabre Nov 05 '17 at 20:49
  • 2
    Dev C++ is an odd choice for modern C programming. I suggest you use either gcc or Visual C. Also, I suggest you recode the entire program again, compiling more often so that you can keep track of the results according to your momentary expectations. We would also appreciate if you format your code properly. – Edenia Nov 05 '17 at 20:56
  • 1
    Also `&*weeklyS` ? The address of the value pointed to by `weeklyS`. `weeklyS` is already a pointer (an address) on the way it is given in the function argument list. No need for `*` and no need for `&` But why the format specifier specifies an integer value and the instance it is getting stored to is a float? – Edenia Nov 05 '17 at 21:04
  • 2
    I agree with what @Edenia says - for a small program it is cheap to compile and check your program at every small change. This is the perfect opportunity to stretch your program with extreme input, such as out of range and corner cases. This remains in your control - you change **one thing at a time** (as with the scientific method) and examine the results. If you leave this essential process until later - you only have a can of worms to unravel! Don't let this put you down - I know how difficult it is. – Weather Vane Nov 05 '17 at 21:05
  • i appreciate you @WeatherVane – rids64 Nov 05 '17 at 21:18
  • I'll never quit, sometimes you just need help and then you have people on here that try to help but are complete a*****es about it. I'm a rookie, who simply needed help. i need to use dev c++ because i want it to challenge myself. – rids64 Nov 05 '17 at 21:20
  • @rids64 then why don't use your favorite text editor and a compiler separately? I don't see what "challenging" dev c++ has about. I should say for a rookie, you are picking up some deeper sides of C programming, so it is a huge start. Being unsystematic in learning C is challenging per se – Edenia Nov 05 '17 at 21:22
  • 1
    Also a quick advice about Stack Overflow: This is a very useful site remotely related to programming issues. It is not facebook or something. You might end up dealing with people you think are assholes, they are here to help. – Edenia Nov 05 '17 at 21:28
  • Commenters can sometimes be a bit curt, but just take their advice on board please. – Weather Vane Nov 05 '17 at 21:30
  • So, hear our advice. Start rewriting your program again, taking into account commenter's notes and if you encounter a problem you cannot solve and cannot find a solution to, ask. If you do that, we will answer to a specific problem, not a code full of such. – Edenia Nov 05 '17 at 21:38
  • regarding: `sales[7] = loadArray(&weeklyS);` indexes in C start with 0 and continue to (number of items in array-1). So this is assigning a value to past the upper bounds of the array. This is undefined behavior and can lead to a seg fault event. – user3629249 Nov 06 '17 at 17:00
  • the posted code contains some 'magic' numbers. 'magic' numbers are numbers with no basis. I.E. 7. 'magic' numbers make the code much more difficult to understand, debug, etc. Suggest using a `enum` statement or `#define` statement to give that 'magic' number a meaningful name, then using that meaningful name throughout the code. – user3629249 Nov 06 '17 at 17:13

1 Answers1

0

the following is not a complete answer, but just the corrections for a single function, with plenty of comments:

The original code:

float loadArray(float *weeklyS)   
{
    float sales;
    int x;

    for ( x = 0; x < 7; x++)
    {
        // have user enter the following
        printf("Enter sales for day %d: ",x  + 1);
        scanf("%d", weeklyS);
    }

    return sales;
}

the corrected code:

give the 'magic' number a meaningful name

#define DAYS_IN_WEEK 7

in 'main()', pass a pointer to the sales[] array.

loadArray( sales );

modify the prototype and the call in 'main()' to match this signature

void loadArray(float *sales)   
{
    // keep the scope of a variable to the minimum
    for ( int x = 0; x < DAYS_IN_WEEK; x++)
    {
        static char *day[] =
        {
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday"
        };

        // have user enter the daily sales
        printf( "Enter sales for %s: ", day[x] );

       // check for errors on calls to system functions
       // note: 'sales+i' advances to pointer 'sales' 
       //      by the length of each 'float' entry 
       //      times the value in 'i'
       // compare the returned value to '1' 
       //      because that is the number of format specifiers
       // use the format specifier '%f' because the input
       //      is to be converted to a 'float' value
       // input sales amount 
       if( 1 != scanf( "%f", sales+i ) )
       {  // then error occurred

           // output your error message 
           // and the system error text 
           // to 'stderr'
           // note: 'perror()' is found in 'stdio.h'
           perror( "scanf failed" ); 

           // note: 'exit()' and EXIT_FAILURE found in 'stdlib.h' 
           // unrecoverable error, exit the program
           exit( EXIT_FAILURE );
       } // end handling error
   } // end for each days' sales
} // end function: loadArray
user3629249
  • 16,402
  • 1
  • 16
  • 17