mardi 21 juin 2016

Can you add variables inside "cout"


I'm new to C++, on the chapter 2 quiz of LearnCpp.com. I'm stuck and have a question. Can you add variables inside a std::cout statement? For example:

The program will not display my answer. The program ends as soon as the user presses enter after inputting the values. Thanks for your help ahead of time.

EDIT: Sorry for not posting the entire code. I'm also new to forums. I added the () like someone suggested. When I ran the program I think I saw it display the answer for a split second and it doesn't show that Press any key to continue. . .

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

int main()
{
double first_value;
double second_value;
char user_operator;

std::cout << "Enter a double value: ";
std::cin >> first_value;
std::cout << "Enter a second double value: ";
std::cin >> second_value;
std::cout << "Enter one of the following (+, -, *, /): ";
std::cin >> user_operator;

if (user_operator == 43 || user_operator == 45
    || user_operator == 42 || user_operator == 47)
    switch (user_operator)
    {
        case 43:
            std::cout << "  " << (first_value + second_value) << "n";
            break;
        case 45:
            std::cout << "  " << (first_value - second_value) << "n";
            break;
        case 42:
            std::cout << "  " << (first_value * second_value) << "n";
            break;
        case 47:
            std::cout << "  " << (first_value / second_value) << "n";
            break;
    }
else std::cout << "Please enter a valid operator.";

return 0;

}


Aucun commentaire:

Enregistrer un commentaire