I have a program that ask a user to input numbers and then a terminal value when finished. It calculates the sum of the numbers as well as count the positive, negative and zero numbers entered. However, I have to rewrite it to also determine what number was entered the most in a row. This is a lab assignment for college and I cant use arrays or read from files. My original code is:
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int num, sum, total_num, total_pos;
int num1, num_comp2, num_row_count;
int num_row_count2, num_row_count_pos;
int total_neg, total_zero;
char choice;
do
{
//initialize counters
sum=0;
total_num=0;
total_pos=0;
total_neg=0;
total_zero=0;
num_row_count=1;
cout<<"#################################################\n";
cout<<"# #\n";
cout<<"# You will be asked to enter a number. #\n";
cout<<"# You may enter positive, negative or decimal #\n";
cout<<"# values including zero. The program will then #\n";
cout<<"# calculate the sum of the numbers and tabulate #\n";
cout<<"# the types of numbers you entered. #\n";
cout<<"# When you are finished, enter 9999. #\n";
cout<<"# #\n";
cout<<"#################################################\n\n";
cout<<"Please enter a number: ";
cin>>num;
while (num != 9999)
{
if (num > 0)
{
num1 = num;
sum = sum + num;
total_num++;
total_pos++;
while (num1 == num)
{
cout<<"Enter another number: ";
cin>>num;
if (num == num1)
{
num_row_count++;
sum = sum + num;
total_num++;
total_pos++;
}
}
}
elseif (num < 0)
{
sum = sum + num;
total_num++;
total_neg++;
cout<<"Enter another number. ";
cin>>num;
}
else
{
total_num++;
total_zero++;
total_pos++;
cout<<"Enter another number. ";
cin>>num;
}
}
//output of the data entered
cout<<endl<<endl;
cout<<"#################################################\n";
cout<<"# Here is your breakdown: #\n";
cout<<"#################################################\n";
cout<<"# #\n";
cout<<"# Total numbers entered: "<<setw(22)<<total_num<<" #\n";
cout<<"# Total positive numbers entered: "<<setw(13)<<total_pos<<" #\n";
cout<<"# Total negative numbers entered: "<<setw(13)<<total_neg<<" #\n";
cout<<"# Total zeros entered: "<<setw(24)<<total_zero<<" #\n";
cout<<"# The sum of all numbers entered: "<<setw(13)<<sum<<" #\n";
cout<<"# #\n";
cout<<"#################################################\n";
cout<<"number in a row "<<num1<<endl;
cout<<"number row amount "<<num_row_count<<endl;
//menu option to continue or end program
cout<<"\nWould you like to enter more numbers? (Y/N) ";
cin>>choice;
}while (choice=='y' || choice=='Y');
return 0;
}
I am a little stumped at how to go about this. I have written a seperate program to isolate the counting of this, but as you can see, I am getting nowhere.
Thanks for the help. After looking at your suggestions and advice from someone else, I figured it out, for the most part. The program now works as I intended, but the only catch is when someone entered more than one number the same amount of times, it only catches the first one. For example if I entered: 55662210, It should say that 5, 6 and 2 were entered the most, but it displays 5. Here is my code:
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
//declare the variables
double num, sum, total_num, total_pos;
int total_neg, total_zero;
double max_num, prev_num;
int curr_count, max_count;
char choice;
do
{
//initialize counters
sum=0;
total_num=0;
total_pos=0;
total_neg=0;
total_zero=0;
//Display with instructions
cout<<"#################################################\n";
cout<<"# #\n";
cout<<"# You will be asked to enter a number. #\n";
cout<<"# You may enter positive, negative or decimal #\n";
cout<<"# values including zero. The program will then #\n";
cout<<"# calculate the sum of the numbers and tabulate #\n";
cout<<"# the types of numbers you entered. #\n";
cout<<"# When you are finished, enter 9999. #\n";
cout<<"# #\n";
cout<<"#################################################\n\n";
//Get input from user
cout<<"Please enter a number: ";
cin>>num;
/*initialize counters & set variables for tracking
numbers entered consecutively and their value */
max_num = num;
max_count = 0;
curr_count = 0;
prev_num = max_num;
//main section
while (num != 9999)
{
// Positive number sorting
if (num > 0)
{
sum = sum + num;
total_num++;
total_pos++;
//determines if numbers entered seqentially are the same
if (num == prev_num)
{
curr_count++;
if (curr_count > max_count)
{
max_count = curr_count;
max_num = num;
prev_num = num;
cout<<"Please enter a number: ";
cin>>num;
}
else
{
prev_num = num;
cout<<"Please enter a number: ";
cin>>num;
}
}
else
{
curr_count = 1;
prev_num = num;
cout<<"Please enter a number: ";
cin>>num;
}
}
//Negative number sorting
elseif (num < 0)
{
sum = sum + num;
total_num++;
total_neg++;
//determines if numbers entered seqentially are the same
if (num == prev_num)
{
curr_count++;
if (curr_count > max_count)
{
max_count = curr_count;
max_num = num;
prev_num = num;
cout<<"Please enter a number: ";
cin>>num;
}
else
{
prev_num = num;
cout<<"Please enter a number: ";
cin>>num;
}
}
else
{
curr_count = 1;
prev_num = num;
cout<<"Please enter a number: ";
cin>>num;
}
}
//Zero sorting
else
{
total_num++;
total_zero++;
total_pos++;
//determines if numbers entered seqentially are the same
if (num == prev_num)
{
curr_count++;
if (curr_count > max_count)
{
max_count = curr_count;
max_num = num;
prev_num = num;
cout<<"Please enter a number: ";
cin>>num;
}
else
{
prev_num = num;
cout<<"Please enter a number: ";
cin>>num;
}
}
else
{
curr_count = 1;
prev_num = num;
cout<<"Please enter a number: ";
cin>>num;
}
}
}
//output of the data entered
cout<<endl<<endl;
cout<<"#################################################\n";
cout<<"# Here is your breakdown: #\n";
cout<<"#################################################\n";
cout<<"# #\n";
cout<<"# Total numbers entered: "<<setw(22)<<total_num<<" #\n";
cout<<"# Total positive numbers entered: "<<setw(13)<<total_pos<<" #\n";
cout<<"# Total negative numbers entered: "<<setw(13)<<total_neg<<" #\n";
cout<<"# Total zeros entered: "<<setw(24)<<total_zero<<" #\n";
cout<<"# Number entered the most in a row: "<<setw(11)<<max_num<<" #\n";
cout<<"# Number of times it was entered: "<<setw(13)<<max_count<<" #\n";
cout<<"# The sum of all numbers entered: "<<setw(13)<<sum<<" #\n";
cout<<"# #\n";
cout<<"#################################################\n";
//menu option to continue or end program
cout<<"\nWould you like to enter more numbers? (Y/N) ";
cin>>choice;
}while (choice=='y' || choice=='Y');
return 0;
}