Home > front end >  How can I make it so user inputs are stored in seperate variables without knowing how many inputs th
How can I make it so user inputs are stored in seperate variables without knowing how many inputs th

Time:12-16

My code is below. I want the user to be able to input any number of operands and then the code asks them to input an operand that number of times. I have that part figured out. How do I store each one of their operands without knowing how many they want in the first place?

for (int i = 0; i < numoperands; i  ){
    cout << "Input Operand: ";
    cin >> ;

CodePudding user response:

Do you need to have a seperate variable for each different input?

If not you can try defining an array outside the for loop then inside the for loop append to the array for every input the user have.

When you want to access the operands, just run another for loop for the length of the array and index it.

CodePudding user response:

You need a bit more "building" blocks for your program to work :

  • loops, allow your code to run the same code multiple times (while and for loop)
  • stop condition, when will you stop with accepting input
  • conversion, from string to integer
  • strings, to hold input

Before you continue though I would recommend you learn a bit more C first. If you don't have any books this might be a good place to start: https://www.learncpp.com/

Example with explanation :

#include <iostream>
#include <string>       // include standard library type for a string.
#include <vector>       // a vector can hold values (or objects) and can grow at runtime

// DO NOT use : using namespace std;
// It is good to unlearn that right away https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

int main()
{
    std::string input;                      // a string to receive input in.
    std::vector<int> values;                // C   does know about arrays, but they are fixed length, 
                                            // vector vector is allowed to grow
    const std::string end_input{ "x" };     // const means you will only read from this variable
                                            // the "x" is used to end the input
       
    // start a do/while loop. Allows you to run the same code multiple times
    do 
    {
        // prompt the user what to do : 
        std::cout << "Enter a number ('"<< end_input << "' to end input) and press return : ";

        // read input to the string so we can check its content
        std::cin >> input;

        // check input end condition    
        if (input != end_input)
        {
            int value = std::stoi(input);   // stoi is a function that converts a string to an integer
            values.push_back(value);        // push back adds value at the end of the vector
        }

    } while (input != end_input); // if condition is not met, then program continues at do (and thus loops)

    // the next lines output the content of the vector to show the input
    
    std::cout << "\n"; // output a new line (avoid std::endl)
    std::cout << "Your input was : \n";

    bool print_comma = false;
    
    for (int value : values) // loops over all values in the vector
    {
        if (print_comma) std::cout << ", ";
        std::cout << value;
        print_comma = true;
    }

    return 0;

}
  • Related