From: Shawn <tre...@gm...> - 2008-02-29 15:46:07
|
I've never tried using arrays before in C++, just am familiar with using them in python. I tried initializing the array as string months and then {"January","February", ect} but the complier told me I needed to set it up in constructor method. I looked that up in my book and came up with this, but now I get the error that January isn't declared. My intention is to have it automatically advance to the next month in the list by months[i] in the inner loop. Am I on the right track? Thanks! #include<iostream> #include<iomanip> using namespace std; int main() { int years = 0; int counter = 0; int month = 0; int average = 0; *class months = {January,February,March,April,May,June,July,August,September,October,November,December};* months++ cout<<"Please enter the # of years.">>endl; for(i=years; i<=years; i++1) { for(i=12; i<=12; i++1) {cout<<"Please enter the rainfall for"<<months[i]<<; cin>>month sum = sum + month counter = counter + 1 } average = sum / counter cout<<"The total number of months was "<<counter<<"."; cout<<"The total inches of rainfall was "<<sum<<"."; cout<<"The average rainfall was "<<average<<"."; return 0; } |
From: Per W. <pw...@ia...> - 2008-02-29 16:09:24
|
You must have missed something in your C++ book. Class of what type? And if you try to get an array of January, February, ... - where is the definition for January? char* months[] = { "January", "February", ... }; In this case, months has a known data type - array of string pointers. In this case, the array elements are not undefined variables, but of directly specified string constants. /pwm On Fri, 29 Feb 2008, Shawn wrote: > I've never tried using arrays before in C++, just am familiar with using > them in python. I tried initializing the array as string months and > then {"January","February", ect} but the complier told me I needed to > set it up in constructor method. I looked that up in my book and came > up with this, but now I get the error that January isn't declared. > > My intention is to have it automatically advance to the next month in > the list by months[i] in the inner loop. Am I on the right track? > > Thanks! > > #include<iostream> > #include<iomanip> > using namespace std; > > > > int main() > { > > int years = 0; > int counter = 0; > int month = 0; > int average = 0; > *class months = > {January,February,March,April,May,June,July,August,September,October,November,December};* > > > months++ > cout<<"Please enter the # of years.">>endl; > for(i=years; i<=years; i++1) > > { > for(i=12; i<=12; i++1) > {cout<<"Please enter the rainfall for"<<months[i]<<; > cin>>month > sum = sum + month > counter = counter + 1 > } > > average = sum / counter > > > cout<<"The total number of months was "<<counter<<"."; > cout<<"The total inches of rainfall was "<<sum<<"."; > cout<<"The average rainfall was "<<average<<"."; > > > > return 0; > } > |
From: Shawn <tre...@gm...> - 2008-02-29 22:12:42
|
It's just going to be a list of the months that the code in the for loop will iterate thru...string data. When they enter one for the outside loop of the year, it will goto the inside loop and each time the loop asks for the average rainfall, I want it to start for Jan, next prompt for Feb, taken directly for the array I'm trying to initialize. Thanks. Per Westermark wrote: > You must have missed something in your C++ book. Class of what type? And > if you try to get an array of January, February, ... - where is the > definition for January? > > char* months[] = { "January", "February", ... }; > > In this case, months has a known data type - array of string pointers. > In this case, the array elements are not undefined variables, but of > directly specified string constants. > > /pwm > > On Fri, 29 Feb 2008, Shawn wrote: > > >> I've never tried using arrays before in C++, just am familiar with using >> them in python. I tried initializing the array as string months and >> then {"January","February", ect} but the complier told me I needed to >> set it up in constructor method. I looked that up in my book and came >> up with this, but now I get the error that January isn't declared. >> >> My intention is to have it automatically advance to the next month in >> the list by months[i] in the inner loop. Am I on the right track? >> >> Thanks! >> >> #include<iostream> >> #include<iomanip> >> using namespace std; >> >> >> >> int main() >> { >> >> int years = 0; >> int counter = 0; >> int month = 0; >> int average = 0; >> *class months = >> {January,February,March,April,May,June,July,August,September,October,November,December};* >> >> >> months++ >> cout<<"Please enter the # of years.">>endl; >> for(i=years; i<=years; i++1) >> >> { >> for(i=12; i<=12; i++1) >> {cout<<"Please enter the rainfall for"<<months[i]<<; >> cin>>month >> sum = sum + month >> counter = counter + 1 >> } >> >> average = sum / counter >> >> >> cout<<"The total number of months was "<<counter<<"."; >> cout<<"The total inches of rainfall was "<<sum<<"."; >> cout<<"The average rainfall was "<<average<<"."; >> >> >> >> return 0; >> } >> >> > > > |
From: Robert A. <ral...@gm...> - 2008-03-01 04:31:33
Attachments:
testcase1.cc
testcase2.cc
|
Hello, A couple of points... as was mentioned you need to figure out what you want months to be. If you want it to be a simple array you can do as was suggested: char* months[] = { "January", "February", ... }; or you could create a class called months which could be much more complicated, which depends upon what you are planning on doing next, so let me give you a couple of quick working solutions for you to play with. Another thing to point out that it's rather bad form to use the same variable name for two nested loops. It may actually work because the inner loop variable will hide the outer loop variable which isn't actually being used but hiding could make it more confusing if you actually did want to use the year loop for something in the future. Also, the << is used repeatably, it's a standard operator , think of it like += where you just keep taking on values at the end, so cout<<"Please enter the # of years.">>endl; should be cout<<"Please enter the # of years."<<endl; Another thing to point out, "using namespace std;" will bring everything from the std name space into the global name space and should be avoided beyond simple test cases, it's find for this but I am going to show you a more deliberate form: using std::cout using std::endl since that's what you're using..that way you won't being in something from std that you didn't intend. also, it's a small performance thing but a good habit to learn... use ++Variable rather than Variable++ if you don't need the old value for anything, otherwise the compiler does something like { int temp ; temp = value ; value += 1 ; return temp } Also, arrays are Zero based in C/C++ so you need to go from 0 to 11 or create months like {"Dummy","January"....} I attached two samples of 'working' code, look if you want.... I know it can be annoying sometimes to get the syntax correct even when you know what you're doing, so hope they help. -Robert On Fri, Feb 29, 2008 at 5:12 PM, Shawn <tre...@gm...> wrote: > > It's just going to be a list of the months that the code in the for loop > will iterate thru...string data. When they enter one for the outside loop > of the year, it will goto the inside loop and each time the loop asks for > the average rainfall, I want it to start for Jan, next prompt for Feb, taken > directly for the array I'm trying to initialize. > > Thanks. > > > > Per Westermark wrote: > You must have missed something in your C++ book. Class of what type? And > if you try to get an array of January, February, ... - where is the > definition for January? > > char* months[] = { "January", "February", ... }; > > In this case, months has a known data type - array of string pointers. > In this case, the array elements are not undefined variables, but of > directly specified string constants. > > /pwm > > On Fri, 29 Feb 2008, Shawn wrote: > > > > I've never tried using arrays before in C++, just am familiar with using > them in python. I tried initializing the array as string months and > then {"January","February", ect} but the complier told me I needed to > set it up in constructor method. I looked that up in my book and came > up with this, but now I get the error that January isn't declared. > > My intention is to have it automatically advance to the next month in > the list by months[i] in the inner loop. Am I on the right track? > > Thanks! > > #include<iostream> > #include<iomanip> > using namespace std; > > > > int main() > { > > int years = 0; > int counter = 0; > int month = 0; > int average = 0; > *class months = > {January,February,March,April,May,June,July,August,September,October,November,December};* > > > months++ > cout<<"Please enter the # of years.">>endl; > for(i=years; i<=years; i++1) > > { > for(i=12; i<=12; i++1) > {cout<<"Please enter the rainfall for"<<months[i]<<; > cin>>month > sum = sum + month > counter = counter + 1 > } > > average = sum / counter > > > cout<<"The total number of months was "<<counter<<"."; > cout<<"The total inches of rainfall was "<<sum<<"."; > cout<<"The average rainfall was "<<average<<"."; > > > > return 0; > } > > > > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > |
From: Shawn <tre...@gm...> - 2008-03-01 04:35:48
|
Thanks for the help everyone...I really appreciate this list! Robert Alatalo wrote: > Hello, > > A couple of points... as was mentioned you need to figure out > what you want months to be. If you want it to be a simple array you > can do as was suggested: > > char* months[] = { "January", "February", ... }; > > or you could create a class called months which could be much more > complicated, which depends upon what you are planning on doing next, > so let me give you a couple of quick working solutions for you to play > with. Another thing to point out that it's rather bad form to use the > same variable name for two nested loops. It may actually work because > the inner loop variable will hide the outer loop variable which isn't > actually being used but hiding could make it more confusing if you > actually did want to use the year loop for something in the future. > > Also, the << is used repeatably, it's a standard operator , think of > it like += where you just keep taking on values at the end, > so > > cout<<"Please enter the # of years.">>endl; > should be > cout<<"Please enter the # of years."<<endl; > > Another thing to point out, "using namespace std;" will bring > everything from the std name space into the global name space and > should be avoided beyond simple test cases, it's find for this but I > am going to show you a more deliberate form: > > using std::cout > using std::endl > > since that's what you're using..that way you won't being in something > from std that you didn't intend. > > also, it's a small performance thing but a good habit to learn... use > ++Variable rather than Variable++ if you don't need the old value for > anything, otherwise the compiler does something like { int temp ; temp > = value ; value += 1 ; return temp } > > Also, arrays are Zero based in C/C++ so you need to go from 0 to 11 or > create months like {"Dummy","January"....} > > > I attached two samples of 'working' code, look if you want.... I know > it can be annoying sometimes to get the syntax correct even when you > know what you're doing, so hope they help. > > -Robert > > > > > On Fri, Feb 29, 2008 at 5:12 PM, Shawn <tre...@gm...> wrote: > >> It's just going to be a list of the months that the code in the for loop >> will iterate thru...string data. When they enter one for the outside loop >> of the year, it will goto the inside loop and each time the loop asks for >> the average rainfall, I want it to start for Jan, next prompt for Feb, taken >> directly for the array I'm trying to initialize. >> >> Thanks. >> >> >> >> Per Westermark wrote: >> You must have missed something in your C++ book. Class of what type? And >> if you try to get an array of January, February, ... - where is the >> definition for January? >> >> char* months[] = { "January", "February", ... }; >> >> In this case, months has a known data type - array of string pointers. >> In this case, the array elements are not undefined variables, but of >> directly specified string constants. >> >> /pwm >> >> On Fri, 29 Feb 2008, Shawn wrote: >> >> >> >> I've never tried using arrays before in C++, just am familiar with using >> them in python. I tried initializing the array as string months and >> then {"January","February", ect} but the complier told me I needed to >> set it up in constructor method. I looked that up in my book and came >> up with this, but now I get the error that January isn't declared. >> >> My intention is to have it automatically advance to the next month in >> the list by months[i] in the inner loop. Am I on the right track? >> >> Thanks! >> >> #include<iostream> >> #include<iomanip> >> using namespace std; >> >> >> >> int main() >> { >> >> int years = 0; >> int counter = 0; >> int month = 0; >> int average = 0; >> *class months = >> {January,February,March,April,May,June,July,August,September,October,November,December};* >> >> >> months++ >> cout<<"Please enter the # of years.">>endl; >> for(i=years; i<=years; i++1) >> >> { >> for(i=12; i<=12; i++1) >> {cout<<"Please enter the rainfall for"<<months[i]<<; >> cin>>month >> sum = sum + month >> counter = counter + 1 >> } >> >> average = sum / counter >> >> >> cout<<"The total number of months was "<<counter<<"."; >> cout<<"The total inches of rainfall was "<<sum<<"."; >> cout<<"The average rainfall was "<<average<<"."; >> >> >> >> return 0; >> } >> >> >> >> >> >> >> ------------------------------------------------------------------------- >> This SF.net email is sponsored by: Microsoft >> Defy all challenges. Microsoft(R) Visual Studio 2008. >> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >> _______________________________________________ >> Dev-cpp-users mailing list >> Dev...@li... >> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >> >> >> |