Sunday, August 2, 2009

C++ help! why a number 1 keeps on popping up in my priming read?

i am putting the 1 in my acummulator....but i don't know...i am just learning...can someone please help me! this is my code:


(by the way, what else do you all see wrong with it?)





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::fixed;


using std::setprecision;








int main()


{


//declare variables


double monthSales = 0.0;


int regions = 1;


double totalSales = 0.0;





while (regions %26lt;= 4)


{


cout %26lt;%26lt; "Enter region: " %26lt;%26lt; regions;


cin %26gt;%26gt; monthSales;





totalSales = totalSales + monthSales;





regions = regions + 1;


//end while


}


cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);


cout %26lt;%26lt; "Total sales: " %26lt;%26lt; totalSales %26lt;%26lt; endl;


return 0;


} //end of main function

C++ help! why a number 1 keeps on popping up in my priming read?
cout %26lt;%26lt; "Enter region: " %26lt;%26lt; regions;





because of that ...





The program is OK ... but there's some changes will make shorter ...





instead of


writing the 4 using::std ....





u can write once : using namespace std;





so, u won't need to write "using::std " else...





and instead of the while loop ... it would be better to use a for loop ... like :





for(regions=1;regions%26lt;=4;regions++)


{


// write your code here without the "regions = regions + 1" line ...


}





and instead of "totalSales = totalSales + monthSales;"


you could write "totalSales += monthSales;"
Reply:Whats wrong with this? The only thing I'm not sure about is the line that reads:





cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);





everything else looks fine.
Reply:cout %26lt;%26lt; "Enter region: " %26lt;%26lt; regions;





Are you asking why this is outputting "Enter region: 1"?





Simply, it's because you're outputting the the variable region.





cout %26lt;%26lt; "Enter region:";





That's what you want. If this isn't what you're asking then I see nothing wrong with your program.


No comments:

Post a Comment