Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

c++ numbering the inputs for simple program

so im new to c++ and im doing a program that takes user inputs for any amount number say 5 so i will get 5 inputs from user and calculate the sum of it ,i did make the program but what i want for the output is say

"Enter Input 1:xx "Enter Input 2:xx

so on and on as the user input say 5 so it goes on for 5 times however my program takes the user input and i enter it, it dosnt say enter input 1 ,so i want to show the enter input 1 enter 2 part hope someone can help me with this sorry for my poor explanation

#include<iostream>
using namespace std;

int main() {
    while (true) {
        // prompts the user to ask how many inputs they want
        int x;

        cout << "Enter input : ";
        cin >> x;

        // If x = -1 dont repeat the loop
        if (x == -1)
            
            break;

        // get the input from above and calculate the total of the input
        int sum = 0;
        for (int i = 0; i < x; i++) {
            int value;
            cin >> value;
            sum += value;
        }

        // Output the total
        cout << "Output total: " << sum << endl;
    }
    system("pause");
    return 0;
}
question from:https://stackoverflow.com/questions/65713180/c-numbering-the-inputs-for-simple-program

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay to start out, there are times when to use break and there are times not to. This scenario is not meant for them. Although, I used one in my code I did it because I am rushing. I would recommend to take the code snippet, learn from it, and see how to optimize it :)

Also, just for future purposes its important to understand your task at hand and be able to communicate it well so others can help debug and answer your question. Heres what I think your question is: "I want to make a program in cpp that allows the user to first submit how many numbers they would like to input. From there I would then ask them for the indicated number of inputs. After gathering all inputs I will then add those numbers together and output the sum. If at any point they decide to type in -1, I will stop asking for inputs and give their sum on the spot."

#include<iostream>
using namespace std;

int main() {
    bool runProgram = true;
    cout << "Hi welcome to my sum calculator program!
";
    cout << "This program will prompt you for a number of inputs and then calculate the total of them.
";
    cout << "If you no longer want to be prompted for numbers at any time type in -1!
";
    cout << "Press enter to begin!
";
    cin.get();
    while (runProgram) {
        // prompts the user to ask how many inputs they want
        int x;

        cout << "How many inputs?
";
        cin >> x;

        // If x = -1 dont repeat the loop
        if (x == -1){
            runProgram = false;
        }else{
                    // get the input from above and calculate the total of the input
        int sum = 0;
        int val = 0;
        for (int i = 0; i < x; i++) {
            cout<< "Input #" << i+1;
            cin >> val;
            if(val == -1){
                runProgram = false;
                break;
            }
            sum += val;
        }
        // Output the total
        cout << "Your Output total is " << sum << endl;
        }
    }
    system("pause");
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...