i have to create a program where you enter a number and the programs adds up to that number for example
enter number: 4 (user input)
your total is: 10
basically (in this example) the program added 1+2+3+4 = 10, but when i execute my program i get
enter number: 4 (user input)
your total is: 8 ***? whats wrong with my code?
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
int count, num, finalans;
cout << "add up til this number : ";
cin >> num;
for (count = 0;count<=num;count++)
finalans = num + count;
cout << "Your total is : " << finalans;
_getch();
return 0;
}
how could i include a do while loop or switch in this code?
example i want this code to keep executing after it asks "do you want to exit program" and you enter no ", it asks again, "enter number:" and executes the program again.
im trying to get the program to keep executing if you dont exit the program after it asks you if you want to exit.
It should be ==, not !=
Otherwise saying "No, I do not want to exit the program" would exit and saying "Yes, I would like to exit" would keep it going.
Thats the problem i am having with the do while, every time i want to continue it does everything correct but it keeps adding up my final answer
example:
Add up til this number: 10(user input)
total: 55
Exit?: N
Add up til this number: 10 (user input)
total: 110****i need it to show me 55 again, not add 55 to the first 55 and give me 110.
Exit?:N
Add up to this number: 10 (user input)
total: 165 ****it keep adding up the totals i just want it to show me 55 everytime if i put 10.
Exit?:
thats why i was asking for help with the do while loop, but thank you all for your help i really appreciate it. i dont know why its so difficult for me to figure this one out? please help.
I think i have found out the problem with your code.As Daleth has found out the problem is with line 9. change line 3 to:int count, num, finalans=0; // initialize finalans as 0 . and line 9 to:finalans =count+finalans;// adds the recurring variable to existing value of finalans
Just follow the rest of the code.
Guess this will be helpful. cheers,. cyber dude
#include <iostream>
usingnamespace std;
int main()
{
char z;
cout <<"\nThis program enables you to add together all the numbers \n\n""in a sequence from 1 up to a number of your choice. \n\n""Press Enter to continue.";
do
{
int b;
cin.get();
cout <<"\nPlease enter the number of your choice: ";
cin >> b;
cin.get();
cout <<"\nYou entered: " <<b;
cin.get();
cout << "\nThe answer is: ";
cout << (1+b)*(b)/2; "\n";
cin.get();
cout<<"\nWant to try it again? y/n ";
cin>>z;
cin.get();
}
while (z == 'y');
return 0;
}
I originally made it to add all the numbers in any sequence. Tell me if you'd like the code. Cheers, Don
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//You can use "using namespace std;" instead of...
int main(){
int value;
int sum=0;
cout<<"Enter your number(or -1 to exit): ";
cin>>value;
while(value!=-1){
if(value==0||value==1){
cout<<"Your total is: "<<value<<endl;
}else{
for(int addNumber=1;addNumber<=value;addNumber++){
sum+=addNumber;
}//end loop for
cout<<"Your total is: "<<sum<<endl;
sum=0; //re-start your sum variable
}//end if...else
cout<<"Enter your number(or -1 to quit): ";
cin>>value;
}//end loop while
return 0; //successful termination
}//end main