whats wrong with my code?

Thanks...
Last edited on
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.
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.
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?
thanks.
Last edited on
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.
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] ;
    }
}
OMG... thank you so much! Can't believe it...
Topic archived. No new replies allowed.