I am having trouble with my else/if statements. When I run my program it only displays the if statement. The else if statements don't work at all, and I get an error if I try to use numbers that fall in the else if guidelines.
Example: If I type (1234). It displays sum(10), Highest(4), Lowest(1);
Example: If I type (5214). It displays sum(12), Highest(4), Lowest(5);
// This program will ask the user to enter a series of single digit numbers with nothing separating them. It will then read the input as a
// C-string or a string object. The program should display the sum of all the single-digit numbers in the string
#include <string>
#include <iomanip>
#include <iostream>
usingnamespace std;
//User input
void userinput()
{
cout << "Enter a series of numbers in a row " << endl;
}
// Main function
int main()
{
//List variables
constint size = 4;
char length[size];
int total=0;
userinput();
cin >> length;
for (int i=0; i<strlen( length ); i++)
{
total += length[i] - '0';
}
cout << "The total is " << total << endl;
if (length[0]<length[1]<length[2]<length[3] )
{cout << "Highest is: " << length[3] << endl;
cout << "Lowest is: " << length[0] << endl;}
elseif (length[1]<length[2]<length[3]<length[0] )
{cout << "Highest is: " << length[0] << endl;
cout << "Lowest is: " << length[1] << endl;}
elseif (length[2]<length[3]<length[0]<length[1])
{cout << "Highest is: " << length[1] << endl;
cout << "Lowest is: " << length[2] << endl;}
elseif (length[3]<length[0]<length[1]<length[2])
{cout << "Highest is: " << length[2] << endl;
cout << "Lowest is: " << length[3] << endl;}
elseif (length[2]<length[1]<length[3]<length[0])
{cout << "Highest is: " << length[0] << endl;
cout << "Lowest is: " << length[2] << endl;}
else
{ cout << "Error" << endl;
}
return 0;
}
The example code my teacher gave me to use to find the highest is lowest doesnt work when I plugged my variable into it, but here it is.
1 2 3 4 5 6 7 8 9 10
int count;
int highest;
highest = numbers[0];
for (count = 1; count < SIZE; count++)
{
if (numbers[count] > highest)
highest = numbers[count];
}
You can't string a series of comparisons together as you're doing.
if (length[0]<length[1]<length[2]<length[3] )
That will result in length[0] being compared with length[1] which will result in a boolean (e.g. true). That boolean will then be compared with length[2] again resulting in a boolean, etc.
Wow I feel stupid, I totally forgot about that. Thank you guys.
I used '&' statements and my code runs good now, but at the end I still get an debug error saying "stack around the variable length was corrupted.
also, if I enter for than 4 numbers it won't work. I know that's because my code can only find the highest and lowest of 4 numbers.
If I do not know how many numbers the user will input, how could I find the highest and lowest values ?
i.e. Instead of using all my if statements , I would be using a code similar to the following (I've tried this code in my function and it didn't work)
1 2 3 4 5 6 7 8 9 10 11 12
int count;
int highest;
highest = length[0];
for (count = 1; count < size; count++)
{
if (length[count] > highest)
highest = length[count];
cout << "Highest is " << length << endl;
}
// This program will ask the user to enter a series of single digit numbers with nothing separating them. It will then read the input as a
// C-string or a string object. The program should display the sum of all the single-digit numbers in the string
#include <string>
#include <iomanip>
#include <iostream>
usingnamespace std;
//User input
void userinput()
{
cout << "Enter a series of numbers in a row " << endl;
}
void asus()
{constint size = 4;
char length[size];
int total=0;
userinput();
cin >> length;
for (int i=0; i<strlen( length ); i++)
{
total += length[i] - '0';
}
cout << "The total is " << total << endl;
int count;
int highest;
int lowest;
highest = length[0];
lowest = length[0];
for (count = 1; count < size; count++)
{
if (length[count] > highest)
highest = length[count];
}
cout << "Highest is " << highest<< endl;
return;
}
for (count = 1; count < size; count++)
{
if (length[count] < lowest)
lowest = length[count];
}
cout << "lowest is " << lowest<< endl;
return;
}
int main()
{
asus();
return 0;
}