Hello,

Is there any infinty sign to assign to a variable ?

I need my program to check if the value of one variable is smaller than the other, but i cant do it cause if i dont set a value to my variable at the beginning of the program it will get a random value from memory and if i would assign a value it should be positive and very big as i dont know what number the client will enter in user input.

I hope I didnt express myself too complex

If the client is entering the numbers it doesn't matter what you assign to them initially. If the client is only entering a single number then I expect that you know what you want to check that against so you can just assign that to the other variable.

The standard class for this purpose is std::numeric_limits, in the <limits> header.

You can basically use it in this way:

template <typename T>
T getMaximumValue() {
  if( std::numeric_limits<T>::has_infinity() )
    return std::numeric_limits<T>::infinity();
  else
    return std::numeric_limits<T>::max();
};

So, if you are using the "double" type for example, you could do:

double getMaximumDouble() {
  if( std::numeric_limits<double>::has_infinity() )
    return std::numeric_limits<double>::infinity();
  else
    return std::numeric_limits<double>::max();
};

To add to what mike_2000_17 mentioned, if you absolutely need a value you can use quiet_NaN . For floating point values it is something that can not be compared. For instance:

#include <iostream>
#include <limits>

int main()
{
    double d = std::numeric_limits< double >::quiet_NaN ();
    if (d == d) 
        std::cout << "d == d => true" << std::endl;
    return 0;
}

Yields nothing.

For non-floating point numbers you are always going to have the problem of the user entering your initialization value unless you use a custom class that indicates a change on write. For all values that an int can represent a user can input any one of them.

Checks are also necessary here ( has_quiet_NaN() ).

@OP:
Can you provide more information about the context and/or use case of your program?

It seems odd to me that you would have to do what you are proposing for what sounds like an extremely simple program...

I m doing a programming exercise i found on internet

Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.

★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes

I m at that modify the program so that it also outputs which person are the least number of pancakes

Thats my code so far, it can determinate who ate most pancakes and output the data but not who ate the leats amount of pancakes

#include <iostream>

using namespace std;

int main()
{
    int person[10]; //Saves user input
    int min;    //Minimum amount of pancakes eaten
    int max = -1; //maximum amount of panckaes eaten
    int whichPerson[10]; //The person who did it
    int thatPersonMax = 0; //Person who ate most pancakes
    int thatPersonMin = 0; //person who ate least pancakes

    for (int n = 0; n < 10; n++)    //Loop for user input
        {
            cout<<"How many pancakes did you eat Person"<<(n+1)<<endl;
            cin>>person[n];     //Gets userinput
            whichPerson[n] = (n + 1);   //Saves the person who entered data


                        if (person[n] > max) {      //determinates who ate most pancakes
                            max = person[n];
                            thatPersonMax = whichPerson[n];
                            }

                        else if (person[n]  < min)  //Should determinate person who ate least pancakes
                            {
                                min = person[n];
                                thatPersonMin = whichPerson[n];
                            }


        }

        cout<<"Person "<<thatPersonMax<<" ate the most pancakes - "<<max<<endl;
        cout<<"Person "<<thatPersonMin<<" ate the least pancakes - "<<min<<endl;
        
        // ^ Outputs the info


    return 0;
}

In that case, have another look at L7Sqr's first post.

All you have to do is store your first input value as the starting value and assume that it's the min value. Then, as you enter more values, you perform additional comparisons and update your stored information as necessary.

Something like this:

#include <iostream>

using std::cout;
using std::cin;

const int mealCount = 10;           //establish a constant for the array size

int main() {
  int breakfast[mealCount] = {0};   //declare the array
  int ateLeast = 0, ateMost = 0;    //variables to track the results of the comparisons

  for (int i = 0; i < mealCount; ++i) {
    cout << "Please enter count " << (i+1) << ": ";
    cin >> breakfast[i];
    if (i > 0) {                    //check to see if this is the first iteration of loop

      if (breakfast[ateLeast] > breakfast[i]) //perform the comparison
        ateLeast = i;               //log the results, if necessary

      else if (...)                 //perform additional comparisons (i.e. most eaten)
        //...
      else
        //...

    } else {                        //initialize the values if this is first iteration of loop
      breakfast[i] = numberEaten;
      ateLeast = 0;
      ateMost = 0;
    }
  }

  //report results...

  return 0;
}

This is just an example, you'll have to figure out how to work something similar into your existing code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.