I had to make a program that calculates how much you gain or loose in a stock markect. I have everything worked out excpet how to use char. I've looked it up but i don't understand anything. Can you help explain it to me?

// Project 6.4. Calculates how much money you have gained, or lost, with a stock.

#include <iostream>

using namespace std; 

float StockName, NumOfStocks, OpenValue, CloseValue, Total;

int main ()
{
    cout << "Please enter the name of the stock. \n" ;
    cin >> StockName;

    cout << "How many stocks do you own? \n" ;
    cin >> NumOfStocks;

    cout << "Please enter its opening value. \n" ; 
    cin >> OpenValue;

    cout << "Please enter its closing value. \n" ;
    cin >> CloseValue;

    Total = (CloseValue - OpenValue) * NumOfStocks ;

    if (Total > 0) 

    cout << "You have gained $" <<Total<< ".\n" ;

    else (Total < 0) ;

    cout << "You have lost $" <<Total<< ".\n" ;

    return 0;
}

char is just like any other Data type. You can do:

char VariableB = 'B';

char K = 'K';

char Zero = '0';

char NULLS = '\0';

//A char is always 1 character in length. An array of chars is called a string:

char Str[] = {'T', 'R', 'I', 'U', 'M', 'P', 'H'};

//which is equivalent to:

string Str = "Triumph";

//You can change chars as well (Just like any other variable):

K = VariableB;

//You can also get user input into a char:


int main()
{
    char VariableChar;
    cout<<"Please enter the first Character of your first name: "<<endl;
    cin>> VariableChar;
}

//It can also be used in a switch case statement and be casted to an int.


int main()
{
    char A = 'A';
    cout<<"The ASCII value of character A is: << (int) A <<endl;

    char Choice = '0';
    cout<<"Please choose a letter from A - C: "<<endl;
    cin>> Choice;
    cin.ignore();

    switch(Choice)
    {
        case 'A': cout<<"You chose A";
        break;

        case 'B': cout<<"You chose "B";
        break;

        case 'C': cout<<"You chose "C";
        break;

        default: cout<<"You didn't enter a character between A and C;
        break;
    }

    if (Choice == 'A')
    {
        cout<<"Choice was equivalent to character A.."endl;
        cout<<"Choice has an ASCII value of: "<< (int) Choice <<endl;
    }
}

I figured it all out and now it works. Thank you c:

Edited 4 Years Ago by vicky30312

This article has been dead for over six months. Start a new discussion instead.