Bryan
Bryan

Reputation: 8697

unable to count numbers of char type

I'm trying to do a certain counter for my assignment but I tried many methods but i still can't figure them out.

My program allows users to find out the type of triangle based on their input.

So i do have 2 main functions to determine the shape of the triangle and to display the counter.

  1. char shapeTriangle(int, int, int); //determine shape of triangle i.e isosceles ('I'), equilateral ('E')

  2. int summaryDisplay(char); // display counter

In my shapeTriangle function, i have a simple if else statement that returns the type based on the user input

char shapeTriangle(int x, int y, int z)
{

char type;

if (x == y && y == z && x == z)

type = 'E';

else if (x == y || y == z || x == z)

type = 'I';
    
return type;
}

Over at my main function, i have loop that allows user to input till one of the value is a 0.

In summaryDisplay, i'm trying to count the number of times a certain triangle is determined

int finalSum(char type)

int eCount = 0,  iCount = 0;

if (type == 'E')

eCount++;

if (type == 'I')

iCount++;
        
cout << "Equilateral" << eCount;
cout << "Isosceles" << iCount;

}

I managed to obtain an output however, the counter returns me some weird values like 540934 or 3453 etc etc which i can't really figure them out.

This is how i attempt to call my function in my int main

int main()

{

int x, y, z;  

do{

cout << "Enter 3 intgers : ";
cin >> x >> y >> z;

//some display output codes

}while ( x != 0 && y != 0 && z != 0); 

finalSum(shapeTriangle(x, y, z));

}

Any help is appreciated.

EDIT 1 : I tried initializing but however, it returns me 0 for all different types.

Upvotes: 0

Views: 87

Answers (4)

Khalil Khalaf
Khalil Khalaf

Reputation: 9407

Just initialize your counters with Zero which will behave as your counter starting value. Like this:

int eCount = 0, iCount = 0;

Also, what would you like type to be if none of your conditions met? Because if you did not initialize type and none of your conditions met (if (x == y && y == z && x == z) and if (x == y || y == z || x == z)) then later both of your counters will stay 0.

Upvotes: 1

alter_igel
alter_igel

Reputation: 7202

It lookes like you've declared eCount and iCount inside the scope of finalSum(). This means that their values, if not initialized, will be garbage from previous activity on the stack, which is what you're seeing. Try declaring them globally, to preserve their values across function calls.

You should make sure that your counting variables are properly declared and initialized before they are incremented, otherwise you are wading into undefined behavior.

Upvotes: 0

Christoph S.
Christoph S.

Reputation: 343

You have to initialize your variables.

Upvotes: 1

Jepessen
Jepessen

Reputation: 12415

You don't have a default value for type. What if all three input values are different? No values are assigned to type in your if statements and this is not initialised.

Try to put a default value like 'U' for unknown...

Upvotes: 0

Related Questions