Hey everyone, I'm looking for some help with a couple of problems in a code I've been working on! Any help would be greatly appreciated! The first thing is that when it goes to report the min/max/etc. stuff (this will make sense in the picture), in a situation where I don't enter any odd numbers (or even), nothing should be reported, but now all I'm getting is an entry of 0 in that situation, and I've really no clue how to fix that. The second issue is that whenever I go to actually enter the integers, regardless of how many I tell it to use, it always stops when I get to 1 before that. Cont..
So for example when I say I want to enter 4 integers, it will stop me and move on after only 3, and it's always like that. Attached is an image of what it should look like (though I mentioned what I need to do about there being no odd/even numbers). And here is the code itself: // Assignment 2.cpp : main project file. #include <StdAfx.h> #include <iostream> #include <string> using namespace std; int main(void) { //Declaring int min=0,max=0,sumo=0,sume=0,n,j=0,totalnum=0,ne=0,no=0; //Creating a cout statement so the user can input the number of integers the program will compile cout << "How many integers do you want to enter > "; cin >> totalnum; if(totalnum<=0)//This is to verify that a positive integer was entered { cout <<"Please enter a number at least greater than 1 "; cin>> totalnum; } else { do { cout << "Enter integer #"<< j++ << " "; cin >> n; if(j==0) { min=max=n; j+1; } else { if(n>max) { max=n; } if(n<min) { min=n; } } if(n%2==0) { sume+=n; ne++; } else { sumo+=n; no++; } }while(j+1<totalnum); } cout << "Max is " << max << "\n"; cout << "Min is " << min << "\n"; cout << "Number of odds is " << no << "\n"; cout << "Number of evens is " << ne << "\n"; cout << "Sum of odds is " << sumo << "\n"; cout << "Sum of evens is " << sume << "\n"; cout << "Integer average is " << (sume +sumo)/totalnum << "\n"; return system("pause"); }
1. The way you've stuctured your code, you will always have some kind of output at the end (printint out the max, min, sum, etc.). Also, if you enter 0 for totalnum, you get a division by zero error (since the bottom code always executes and you are comptuing the mean using totalnum). 2. At the start of the program code, you initialize j to 0. At the beginning of the do-while loop, you increment j (it goes from [1,2,3] if totalnum = 4), so if totalnum = 4, j+1 will go from [2,3,4] before breaking out of the loop; it will only execute 3 times instead of the expected 4 :( Number 2 is fixed if you post-increment j within the while() condition at the end: http://ideone.com/Oud8w To solve number 1, use logical strucutres in your code (if statements, loops, etc) to check the input; if you don't want the final part of your code to execute if no (# of odd numbers is 0) or if a bad totalnum was input, then you can use something like if (no != 0 && ne != 0 && totalnum > 0) // if there is no bad input, output the final result { // output stuff } http://ideone.com/20Ixg
First, wouldn't it be easier to use a for loop to enter your integers rather than incrementing a counter and performing a check? Example (in pseudo code). for c = 1 to totalnum; print "Enter integer # " c; input n
@jagatuba I thought the do {} while () part was already doing that :-P
ahh.... right.
Yes, but it doesn't seem that efficient to me, but I'm not a C expert like yourself. Also why not use an int array to store the entries. That way all entries are entered and then calculations can occur afer the entry loop.
Yes I would prefer the for loop in this instance.
Also, why only positive integers? the screenshot show both positive and negative integers.
right; only totalnum needs to be positive
Just formatting. Change it so it asks for #1 #2 etc instead of #0 #1 etc and made Max prin to next line.
Oh right DUH.
Also where is min and max of odd and even; and average of od and even?
Remove min, max, and integer average display and it will be identical to the screen shot.
Join our real-time social learning platform and learn together with your friends!