whats wrong with my code?

Oct 1, 2015 at 12:56am
Thanks...
Last edited on Oct 1, 2015 at 3:28am
Oct 1, 2015 at 1:47am
Things that begin a group of braces {} do not start off with semicolons.

It is not:

1
2
3
if();
{
}


It is:

1
2
3
if()
{
}


You do this mistake for all of your if and while loops.
Oct 1, 2015 at 2:17am
Thank you Ispil! I did as you pointed out, but now the 1s are 0s so something isn't working right... Any help would be appreciated. Thank you.
Oct 1, 2015 at 2:32am
Well, for one, when you declare the variable containers on line 6, you then immediately check to see whether it is equal to 0 in a while loop. What value does containers actually hold at that point? When you subtract 50?
Oct 1, 2015 at 2:43am
thanks.
Last edited on Oct 1, 2015 at 3:28am
Oct 1, 2015 at 3:02am
The problem with your code is that it runs sequentially- from the top of main() to the bottom. Your while() loop and everything else is being ran before you get values for anything. Consider moving it until after you get all of the user inputs.
Oct 1, 2015 at 3:12am
Move lines 46-63 (get user input) to before line 19 (process user input).

Division with remainder is a handy substitute for repeated subtraction.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
    const int NSIZES = 4 ;
    const int container_size[NSIZES] = { 50, 20, 5, 1 } ; // descending order; the last size is 1
    const std::string container_name[NSIZES] = { "huge", "large", "medium", "small" } ;

    int nitems ;
    std::cin >> nitems ;

    for( int i = 0 ; i < NSIZES ; ++i )
    {
        const int nc = nitems / container_size[i] ;
        if( nc > 0 ) std::cout << nc << ' ' << container_name[i] << '\n' ;
        nitems %= container_size[i] ;
    }
}
Oct 1, 2015 at 3:27am
OMG... thank you so much! Can't believe it...
Topic archived. No new replies allowed.