14 Replies - 420 Views - Last Post: 04 March 2013 - 02:13 AM Rate Topic: -----

#1 nshah93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 02:46 PM

The question is:
Write a program that reads an unspecified number of integers, determines how many positive and
negative values have been read, and computes the total and average of the input values(not
counting zeros). Your program ends with the input 0. Display the average as a double. Here is a
sample run:
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3.
The number of negatives is 1.
The total is 5.
The average is 1.25.

This is the program which I wrote:
#include<iostream>
using namespace std;
int main (){
   int x;
   int numofpos=0;
   int numofnegs=0;
   int total=0;
   int sum=0;
   int average=0;
   cout<< "Please enter a number:";
   cin>>x;
   while (x!=0){
     if (x >0){
       numofpos=numofpos++;
     }
     if (x <0){
       numofnegs=numofnegs++;
     }
    sum +=x;
    }
   cout<< "The number of positive numbers is"<< numofpos<<endl;
   cout<< "The number of negative numbers is"<< numofnegs<<endl;
   cout<< "The total number is"<<sum<<endl;
   cout<< "The average is"<<(sum)/(numofpos+numofnegs)<<endl;
  return 0;
}


When I run the program, I am asked to only input the numbers, but I do not get the output which I asked for. Please keep it basic!

Is This A Good Question/Topic? 0
  • +

Replies To: C++ program only asks for input doesn't input any numbers

#2 nshah93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 02:57 PM

View Postnshah93, on 28 February 2013 - 02:46 PM, said:

The question is:
Write a program that reads an unspecified number of integers, determines how many positive and
negative values have been read, and computes the total and average of the input values(not
counting zeros). Your program ends with the input 0. Display the average as a double. Here is a
sample run:
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3.
The number of negatives is 1.
The total is 5.
The average is 1.25.

This is the program which I wrote:
#include<iostream>
using namespace std;
int main (){
   int x;
   int numofpos=0;
   int numofnegs=0;
   int total=0;
   int sum=0;
   int average=0;
   cout<< "Please enter a number:";
   cin>>x;
   while (x!=0){
     if (x >0){
       numofpos=numofpos++;
     }
     if (x <0){
       numofnegs=numofnegs++;
     }
    sum +=x;
    }
   cout<< "The number of positive numbers is"<< numofpos<<endl;
   cout<< "The number of negative numbers is"<< numofnegs<<endl;
   cout<< "The total number is"<<sum<<endl;
   cout<< "The average is"<<(sum)/(numofpos+numofnegs)<<endl;
  return 0;
}


When I run the program, I am asked to only input the numbers, but I do not get the output which I asked for. Please keep it basic!


Please excuse the link part on the bottom not sure how that got there!
Was This Post Helpful? 0
  • +
  • -

#3 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 771
  • Joined: 27-June 09

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 03:05 PM

See comments

   cout<< "Please enter a number:";
   cin>>x;
//suppose we enter 5, so 5 is stored in x.

/********************************/
//This code will repeat until x is zero,
//but there is nothing in this code that
//changes x so it will never be 0.  X will
//remain 5.
   while (x!=0){
     if (x >0){
       numofpos=numofpos++;
     }
     if (x <0){
       numofnegs=numofnegs++;
     }
    sum +=x;
    }
/**********************************/


This post has been edited by mojo666: 28 February 2013 - 03:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 nshah93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 03:09 PM

View Postmojo666, on 28 February 2013 - 03:05 PM, said:

See comments

   cout<< "Please enter a number:";
   cin>>x;
//suppose we enter 5, so 5 is stored in x.

/********************************/
//This code will repeat until x is zero,
//but there is nothing in this code that
//changes x so it will never be 0.  X will
//remain 5.
   while (x!=0){
     if (x >0){
       numofpos=numofpos++;
     }
     if (x <0){
       numofnegs=numofnegs++;
     }
    sum +=x;
    }
/**********************************/



sorry if i sound stupid... but I thought that when i wrote x!=0 that meant that x could never be 0? Im really new with this (only been programming for the last 3 weeks) so i am kind of lost!
Was This Post Helpful? 0
  • +
  • -

#5 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 771
  • Joined: 27-June 09

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 03:25 PM

You are slightly off.

while(/*condition*/)
{
	//body
}



A while loop checks if the condition is true. if it is true, then it executes the code in its body. Then it checks the condition again. It keeps repeating this as long as the condition is true. Your condition is checking if x is equal to 0. Since it is not, x!=0 is true and the program executes the body of the while loop, incrementing sum and numPositive or numNegative. Then, it checks if x is equal to 0 and repeats. Since there is nothing that changes x, x will never be 0 and the body of the loop just repeats over and over and over.

Basically, you should add a prompt at the end of the loop so you can enter a new value for x. When you are done entering numbers, enter 0 and then the program will move on to your output.
Was This Post Helpful? 0
  • +
  • -

#6 nshah93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 03:28 PM

View Postmojo666, on 28 February 2013 - 03:25 PM, said:

You are slightly off.

while(/*condition*/)
{
	//body
}



A while loop checks if the condition is true. if it is true, then it executes the code in its body. Then it checks the condition again. It keeps repeating this as long as the condition is true. Your condition is checking if x is equal to 0. Since it is not, x!=0 is true and the program executes the body of the while loop, incrementing sum and numPositive or numNegative. Then, it checks if x is equal to 0 and repeats. Since there is nothing that changes x, x will never be 0 and the body of the loop just repeats over and over and over.

Basically, you should add a prompt at the end of the loop so you can enter a new value for x. When you are done entering numbers, enter 0 and then the program will move on to your output.

But if i do that, would i have to enter the numbers one after another, I need to be able to input the numbers all at once.
Was This Post Helpful? 0
  • +
  • -

#7 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 771
  • Joined: 27-June 09

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 03:41 PM

Oh, I see what you are trying to do. If you want to read it all in one go, you will have to make x a string. When x is an int, I am pretty sure something like "1 2 4 -9 0" would only store the 1 in x. When x is a string, you will have to loop through the characters and convert them to numbers one at a time. You could probably find a split or tokenizer function for this.
Was This Post Helpful? 0
  • +
  • -

#8 nshah93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 03:52 PM

View Postmojo666, on 28 February 2013 - 03:41 PM, said:

Oh, I see what you are trying to do. If you want to read it all in one go, you will have to make x a string. When x is an int, I am pretty sure something like "1 2 4 -9 0" would only store the 1 in x. When x is a string, you will have to loop through the characters and convert them to numbers one at a time. You could probably find a split or tokenizer function for this.

hmm we havent learnt that in class yet, so i dont think I would be able to use that, is there anything simpler than that?
Was This Post Helpful? 0
  • +
  • -

#9 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 04:14 PM

View Postnshah93, on 28 February 2013 - 10:52 PM, said:

View Postmojo666, on 28 February 2013 - 03:41 PM, said:

Oh, I see what you are trying to do. If you want to read it all in one go, you will have to make x a string. When x is an int, I am pretty sure something like "1 2 4 -9 0" would only store the 1 in x. When x is a string, you will have to loop through the characters and convert them to numbers one at a time. You could probably find a split or tokenizer function for this.

hmm we havent learnt that in class yet, so i dont think I would be able to use that, is there anything simpler than that?

Well, instead of just having x, you could have x1, x2, x3, x4 etc for how many numbers will be inputed. You can then use a cin like how you use cout for multiple outputs: cin >> x1 >> x2 >> x3 >> x4 >> x5; Then each space acts like a deliminator, so if you input 1 2 -1 3 0 then x1 = 1, x2 =2, x3 = -1, x4 = 3 and x5 = 0. You will need to improve your loop to account for the multiple xs, I think this would work:
while (x1!=0){
     if (x1 >0){
       numofpos=numofpos++;
     }
     if (x1 <0){
       numofnegs=numofnegs++;
     }
    sum +=x;
 }
while (x2!=0){
     if (x2 >0){
       numofpos=numofpos++;
     }
     if (x2 <0){
       numofnegs=numofnegs++;
     }
    sum +=x;
 }
etc etc



That should work for you now :)
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1331
  • View blog
  • Posts: 4,565
  • Joined: 19-February 09

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 04:15 PM

If the prompt is outside the loop, and the stream read inside the loop it should work ok.
Was This Post Helpful? 0
  • +
  • -

#11 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 771
  • Joined: 27-June 09

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 04:19 PM

Are you sure you need to enter all the numbers in one line? I know it looks that way on the sample run, but the problem does not explicitly state this as a requirement. I think you can enter them one at a time.
Was This Post Helpful? 0
  • +
  • -

#12 nshah93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Re: C++ program only asks for input doesn't input any numbers

Posted 28 February 2013 - 04:19 PM

Thank you to all that helped! But I compiled this code which was much more easier for me to understand! But thank you all for your help! I really appreciate it!
#include<iostream>
using namespace std;
int main(){
int number=0,positive=0,negative=0,sum=0;
    cout<<"Start entering numbers: ";
    while (true){
        cin>>number;
        if (number==0){
        break;
        }
        if (number>0){
        positive++;
        }
        else{
        negative++;
        }
        sum+=number;
    }
    cout<<positive<<" positive numbers"<<endl;
    cout<<negative<<" negative numbers"<<endl;
    cout<<"total: "<<sum<<endl;
    cout<<"average: "<<((float)sum)/(positive + negative)<<endl;
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#13 aliraza1944  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-March 13

Re: C++ program only asks for input doesn't input any numbers

Posted 02 March 2013 - 08:07 AM

Here solved
#include<iostream>
using namespace std;

int main ()
{

   int x=1;

   int numofpos=0;

   int numofnegs=0;

   int total=0;

   int sum=0;

   int average=0;

   cout<< "Please enter a number:";

   

   while (x!=0){
	cin>>x;
     if (x >0){

       numofpos=numofpos++;

     }

     if (x <0){

       numofnegs=numofnegs++;

     }

    sum +=x;

    }

   cout<< "The number of positive numbers is : "<< numofpos<<endl;

	cout<< "The number of negative numbers is : "<< numofnegs<<endl;

	cout<< "The total number is : "<<sum<<endl;

	cout<< "The average is : "<<(sum)/(numofpos+numofnegs)<<endl;

for(;;)/>;
}

Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: C++ program only asks for input doesn't input any numbers

Posted 02 March 2013 - 08:50 AM

numofpos=numofpos++;

numofnegs=numofnegs++;


I am 99% sure both of these result in undefined behavior, making this "solved" program wrong.
Was This Post Helpful? 2
  • +
  • -

#15 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: C++ program only asks for input doesn't input any numbers

Posted 04 March 2013 - 02:13 AM

Well, for that you could either do this

if (x >0){

       numofpos++;

     }

     if (x <0){

       numofnegs++;
     }


Or

if (x >0){

     numofpos=numofpos + 1;

     }

     if (x <0){

     numofnegs=numofnegs+1;
     }

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1