If/Else if statements not working with string.

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);



I must use a string or cstring.



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  // 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>
using namespace std;

//User input
	void userinput()
	{

	cout << "Enter a series of numbers in a row " << endl;
	
	}

// Main function
int main()
{  
	
	//List variables
	const int 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;}
	
	else if (length[1]<length[2]<length[3]<length[0] )
		{cout << "Highest is: " << length[0] << endl;
	     cout << "Lowest is:  " << length[1] << endl;}

	else if (length[2]<length[3]<length[0]<length[1])
		{cout << "Highest is: " << length[1] << endl;
	     cout << "Lowest is:  " << length[2] << endl;}
	
	else if (length[3]<length[0]<length[1]<length[2])
		{cout << "Highest is: " << length[2] << endl;
		 cout << "Lowest is:  " << length[3] << endl;}
	
	else if (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];
}

This statement

if (length[0]<length[1]<length[2]<length[3] )

will be always equal to true if length[3] is not equal to zero.

I think you meant

if (length[0]<length[1] && length[1]<length[2] && length[2]<length[3] )
Last edited on
Your if statements are defective.

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.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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>
using namespace std;

//User input
	void userinput()
	{
	cout << "Enter a series of numbers in a row " << endl;
	}

	void asus()
	{const int 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[1]<length[2] && length[2]<length[3]  )
		{cout << "Highest is: " << length[3] << endl;
	     cout << "Lowest is:  " << length[0] << endl;}
	
	else if (length[0]<length[2] && length[2]<length[1] && length[1]<length[3]  )
		{cout << "Highest is: " << length[0] << endl;
	     cout << "Lowest is:  " << length[1] << endl;}

	else if (length[1]<length[3] && length[3]<length[2] && length[2]<length[0]  )
		{cout << "Highest is: " << length[0] << endl;
	     cout << "Lowest is:  " << length[1] << endl;}
	
	else if (length[1]<length[2] && length[2]<length[3] && length[3]<length[0]  )
		{cout << "Highest is: " << length[0] << endl;
	     cout << "Lowest is:  " << length[1] << endl;}
	
	else if (length[2]<length[3] && length[3]<length[0] && length[0]<length[1] )
		{cout << "Highest is: " << length[1] << endl;
	     cout << "Lowest is:  " << length[2] << endl;}

	else if (length[2]<length[0] && length[0]<length[3] && length[3]<length[1]  )
		{cout << "Highest is: " << length[1] << endl;
	     cout << "Lowest is:  " << length[2] << endl;}
	
	else if (length[1]<length[3] && length[3]<length[0] && length[0]<length[2]  )
		{cout << "Highest is: " << length[1] << endl;
	     cout << "Lowest is:  " << length[2] << endl;}

	else if (length[1]<length[0] && length[0]<length[3] && length[3]<length[2]  )
		{cout << "Highest is: " << length[1] << endl;
	     cout << "Lowest is:  " << length[2] << endl;}


	else if (length[2]<length[1] && length[1]<length[3] && length[3]<length[0] )
		{cout << "Highest is: " << length[0] << endl;
		 cout << "Lowest is:  " << length[2] << endl;}
	
	else if (length[2]<length[3] && length[3]<length[1] && length[1]<length[0]  )
		{cout << "Highest is: " << length[0] << endl;
	     cout << "Lowest is:  " << length[2] << endl;}

	else
	{ cout << "Error" << endl;
	}
	return;
	}


int main()
{  
	asus();
	
	
	return 0;
}
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;

}


Last edited on
cout << "Highest is " << length << endl;

Why are you outputting length? The variable that holds your highest value is "highest".
I put "highest" instead of length and still wont work

here's my new code

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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>
using namespace std;

//User input
	void userinput()
	{
	cout << "Enter a series of numbers in a row " << endl;
	}

	void asus()
	{const int 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;
}
Topic archived. No new replies allowed.