whats the error in this program?

Oct 30, 2015 at 12:36pm
in this program we are supposed to enter numbers greater than or equal to 0. if we enter a number lesser than 0, the program should end and then display the total amount of positive numbers that i entered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main ()
{
	int number=0,total=0;
	
	cout<<"Type a number : ";
	cin>>number;
	
	while(number>=0)
	{
		cout<<"Type a number : ";
		cin>>number;
		
		total=number+1;
	}
	
	cout<<total;
	
	return 0;
}
Oct 30, 2015 at 12:46pm
Move line 15 before line 12.

And: total+=number+1;
Last edited on Oct 30, 2015 at 12:47pm
Oct 30, 2015 at 1:42pm
something is still not right. let me rephrase what i mentioned above. if i enter any 5 positive numbers and after that if i enter -1, the program should display 5. similarly if i enter any 15 positive numbers the program should display 15
Oct 30, 2015 at 4:12pm
You still have work to do. Providing cin>> with a numeric string will not automatically convert it to its intended binary value. Typing "-1" will not give you a negative integer as it is still in string form. Instead you get a positive value of 0x2D31. I should also mention entering any more than 8 digits will likely cause this code to crash due to a memory access violation.
Oct 30, 2015 at 4:23pm
closed account (E0p9LyTq)
kamilhassaan wrote:
if i enter any 5 positive numbers and after that if i enter -1, the program should display 5. similarly if i enter any 15 positive numbers the program should display 15


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main ()
{
   int number = 0;
   int total = 0;

   while(true)
   {
      std::cout<<"Type a number : ";
      std::cin>>number;

      if (number < 0)
      {
         break;
      }

      total++;
   }

   std::cout << "\nThe total is: " << total << std::endl;

   return 0;
}
Last edited on Oct 30, 2015 at 4:26pm
Oct 30, 2015 at 4:46pm
while( std::cin>>number and number>=0 )
Oct 30, 2015 at 4:48pm
This code should do the job

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
	int total = 0;
	// number should be a char array
	char number[24];

	while (true)
	{
		cout << "Type a number : ";
		cin >> number;
		//check for negative number
		if (number[0] == '-')
			break;
		 total++;
	}

	cout << total;

	return 0;

Oct 30, 2015 at 5:13pm
doug88dg is incorrect, cin will read negative values.

also, reading the comments here i feel the need to point out there are two type of while loop in the world. "while" and "do while" (aka repeat until)

if you definitely want to execute the loop at least once, go for do while, otherwise use the normal while.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main ()
{
   int number=0,total=0;

   do
   {
      cout<<"Type a number : ";
      cin>>number;

      if (number>=0)
         ++total;

   } while(number>=0)

   cout << total;

   return 0;
}

Oct 30, 2015 at 7:02pm
I stand corrected. Jaybob66's code will work, though I should point out that problems may occur if anything other than a number is given. This code is a little bulkier, but is more stable.
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
34
35
36
37
38
#include <iostream>

using namespace std;

int main()
{
    int total = 0;
    int number;
    char buf[16] = { 0 };
    bool sign;

    do
    {
        cout << "Type a number : ";
	cin >> buf;
	number = 0;
	sign = false;
		
	for (int i = 0; i < 16; i++)
	{
	        if (buf[i] >= 0x30 && buf[i] <= 0x39)
		    number = number * 10 + buf[i] - 0x30;
		if (buf[i] == '-' && number == 0)
		    sign = true;
		buf[i] = 0;
         }
		
	number = sign ? -number : number;
	
	if (number > 0)
		total++;
			
	}while (number >= 0);

	cout << total;

	return 0;
}


Jaybob66, thanks for the correction.
Oct 31, 2015 at 4:32pm
I appreciate you guys trying to help, but the thing is, I started C++ less than a week ago. I am only aware of the following functions;
1. If-Else
2. Nested if
3. While loop
4. For loop

I only know the basics of these functions. And my teacher didn't teach us the "Do-While Loop".
My teacher did not tell us anything about using "std" anywhere else other than "using namespace std;".

So, can you guys please make it simple? Thank you!
Topic archived. No new replies allowed.