Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to make choices ic C++

+16 votes
asked Oct 13, 2024 by Prateik Shrihari Pravin (270 points)

3 Answers

+2 votes
answered Oct 14, 2024 by Sumama (180 points)
#include <iostream>

int main(){

int x = 10;

if(x>10){ std::cout<<"x is greater than 10";}

elseif(x<10){ std::cout<<"x is smaller than 10";}

else {"x is equal to 10";}

return 0;

}
+1 vote
answered Oct 14, 2024 by Peter Minarik (101,340 points)

How about this one?

main.cpp

#include "QuestionWithAnswers.h"
#include <iostream>

int main()
{
    std::string answers[] = { "Pizza", "Burger", "Hot dog" };
    QuestionWithChoices question = QuestionWithChoices("What's your favourite fast food?", answers, 3);
    
    int choice = question.GetAnswer();
    if (choice == -1)
    {
        std::cout << "Invalid choice." << std::endl;
        return -1;
    }
    
    std::cout << "Your favourite food is " << answers[choice - 1] << '.' << std::endl;
    return 0;
}

QuestionWithAnswers.h

#pragma once

#include <string>

class QuestionWithChoices
{
private:
    std::string question;
    const std::string * answers;
    size_t answerCount;

public:
    QuestionWithChoices(const std::string & question, const std::string * answers, size_t answerCount);
    int GetAnswer();
};

QuestionWithAnswers.cpp

#include "QuestionWithAnswers.h"
#include <cstdlib>
#include <iostream>

QuestionWithChoices::QuestionWithChoices(const std::string & question, const std::string * answers, size_t answerCount)
{
    this->question = question;
    this->answers = answers;
    this->answerCount = answerCount;
}

int QuestionWithChoices::GetAnswer()
{
    std::cout << question << std::endl;
    
    for (size_t i = 0; i < answerCount; i++)
        std::cout << i + 1 << ". " << answers[i] << std::endl;
    
    std::cout << "? ";

    std::string choice;
    std::cin >> choice;
    int result = std::atoi(choice.c_str()); // atoi returns 0 if it cannot get a number, so let's keep the valid answers between 1 and answerCount.
    if (result < 1 || result > answerCount)
        return -1;
    
    return result;
}
+2 votes
answered Oct 18, 2024 by Hamza Khan (180 points)

You can make choices in C++ by using conditional statements like if, else-if, else and switch:

Syntax for if, else-if, else:

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;

    if (number > 0) {
        cout << "The number is positive." << endl;
    } else if (number < 0) {
        cout << "The number is negative." << endl;
    } else {
        cout << "The number is zero." << endl;
    }

    return 0;
}

Syntax for Switch

#include <iostream>
using namespace std;

int main() {
    int choice;
    cout << "Choose an option (1-3): ";
    cin >> choice;

    switch (choice) {
        case 1:
            cout << "You chose option 1." << endl;
            break;
        case 2:
            cout << "You chose option 2." << endl;
            break;
        case 3:
            cout << "You chose option 3." << endl;
            break;
        default:
            cout << "Invalid choice! Please choose a number between 1 and 3." << endl;
    }

    return 0;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...