Saturday, May 22, 2010

Help with C++?

ok..I posted a question about this earlier...and i notice i wasn't very clear or helpful...well here it is again...





Problem: Write a program that asks the user to enter some positive numbers until 0 is entered. The program then reports the total of all even numbers entered. The users can enter as many numbers as they want. When negative numbers are entered, present a message to the user for the error.





i also posted what i've done so far...and for some reason its not working...i'm also having trouble trying to get the program to show only even numbers..i appreciate the help...thanks





int main()


{





double amount=0.0;





do


{





cout%26lt;%26lt; "Please enter some numbers, enter 0 once you don't want to enter anymore numbers: "%26lt;%26lt;endl;


cin %26gt;%26gt; amount;





if (amount%26lt;0)


{


cout %26lt;%26lt; "ERROR! Please enter a number higher than 0: " %26lt;%26lt;endl;


cin %26gt;%26gt; amount;


}








} while (amount !=0);





return 0;





}

Help with C++?
Use an iteger and get the remainder of 2 to determine whether its even or odd.








#include %26lt;iostream%26gt;





using namespace std;





int main()


{


int UserInput = 0, EvenCount = 0;





do /*** could actually just enter while true here if we wanted, because we break when the user enters 0**/


{


cout %26lt;%26lt; "Enter a number or 0 to exit "; /***ask the user for a number**/


cin %26gt;%26gt; UserInput; /**GET THE NUMBER**/


if(UserInput == 0) /***break if he enters 0***/


{


break;


}


else if(UserInput %26lt; 0) /**report an error if its negative and return to the loop**/


{


cout %26lt;%26lt; "You must enter a positive number";


continue;


}


else


{


if((UserInput % 2) == 0) //if its even then add it to even count


EvenCount++;


}


}while(UserInput != 0);


cout %26lt;%26lt; "You entered " %26lt;%26lt; EvenCount %26lt;%26lt; " Even Numbers" %26lt;%26lt; endl;


return 0;


}
Reply:You need to change the "while" statement into an "if" statement. Something like this:


double total = 0;


if (amount != 0 %26amp;%26amp; amount % 2 = 0)


{


total += amount;


return;


{
Reply:you should use an unsigned long instead of double, because doubles can't really be even or odd if they have fractional parts, and you're not handing negatives so take the extra 2billion of range by using the long unsigned. The following I just wrote and tested. Learn well. Please don't just copy, actually work through and learn from it.





#include %26lt;iostream%26gt;


#include %26lt;climits%26gt;





using namespace std;





int main(void)


{


unsigned long amount = 0;


bool done = false;





cout %26lt;%26lt; "\n\tNumber adding program. Input 0 (zero) to"


"end.\n" %26lt;%26lt; endl;





while(!done)


{


unsigned long num;


cout %26lt;%26lt; "Input number %26gt;0: ";


cin %26gt;%26gt; num;


while(cin.fail())


{


cin.clear();


cin.ignore(INT_MAX, '\n');


cout %26lt;%26lt; "\nThat number wasn't %26gt;=0. \n\n";


cout %26lt;%26lt; "Input number %26gt;0: ";


cin %26gt;%26gt; num;


}


if(num==0)


{


done = true;


}


amount += ( num % 2 ) ? 0 : num;


}


cout %26lt;%26lt; "\nThe total of even numbers is "


%26lt;%26lt; amount %26lt;%26lt; '.' %26lt;%26lt; endl;


return 0;


}
Reply:well, i am not able to put that code into something and compile it yet, but why are you using double for the amount.. i don't think you would need that...


No comments:

Post a Comment