need help with my code please

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;
}

Try this on line 9.
 
finalans += count; //equivalent to finalans = finalans + count 


This will add count to the current value of finalans and assign the result to finalans.

Your current instruction simply assigns new values to finalans, so when count = num, finalans = 4+4.

Oh, and you should initialize finalans with 0 to avoid any possible junk assignment when it was declared.
Last edited on
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.
You could try something like this

1
2
3
4
5
6
7
8
9
char choice;

do
{
// Your code

cout << "Do you want to exit program? (Y/N)";
cin >> choice;
}while(choice == 'N');
Kinley, don't you mean:
 
   }while ((choice != 'N') || (choice != 'n'));
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.
Last edited on
Oh, haha. I guess I misread that line. Thanks for correcting me.
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.
Last edited on
It is obvious that the variable that stores the sum is not set to zero before each new iteration.

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
Last edited on
I DID have finalans = 0 , but i needed to put it before the FOR function and after the DO, because the way i had it declared was

1
2
3
4
5
6
7
8
int finalans, count, num;
finalans = 0;
do
{
code // but i realized i needed to declare it here in this code
}
for()
code


and it wasnt working, but then i realized i hadnt tried putting it in between do and for and it worked. thanks again guys.
Last edited on
@Fyah
Probably u want this>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//codded by_Lutu Putu_(From Independent university, Bangladesh)
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int n, i, sum=0;
    char ch;
    
    do{
    cout<<"Enter number: ";
    cin>>n;
    for(i=1; i<=n; i++)
             sum+=i;
    
   
    cout<<"The output is: "<<sum;
    sum=0;
    cout<<"\n\nDo you have number?\n";
    cin>>ch; 
    cout<<endl;
    }while(ch=='Y' || ch=='y');
    
    getch();
    system ("Pause");
    return 0;
}
@Sam99 please do not post or suggest non-standard code. <conio.h> is nonstandard:
http://en.wikipedia.org/wiki/Conio.h

Also please do not recommend use of "system":
http://www.cplusplus.com/forum/articles/11153/
Last edited on
Then what I should? :)
Hi Fyah, this works well using the brilliant Gauss equation mentioned in TV's "Person of Interest"!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace 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
Last edited on
@L B
You mean #include <limits> instead of #include <conio.h> ?
Last edited on
I mean don't #include <conio.h> at all.
I think this is a simple way



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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
Topic archived. No new replies allowed.