0

Very new to coding so there might be a better way to phrase this. This issue you be obvious to most but I got to get better one step at a time. Any other advice to improve this code is very much welcomed.

Example:Welcome to Motown records Keth Kennedy, What will store will you be shopping with?

I Pass fff fff

The program will print all the cout below and pass all the other variables 0. so it will say you have spent 0 dollors.

#include <iostream> //Allows for input and output stream.
using namespace std; //Makes it so you don't have to add std:: 

int main() {
    //All variables used in this binary.
    string compname = "Motown Records";
    const string Card = "Apple Card";
    string Fname = "Keth";
    string Lname = "Kennedy";
    string expirationdate = "09/25";
    double Maxbalance = 300.00f;
    double cbalance = 250.00f;
    double tamount;
    double nbalance;
    string storename;
    string dot;
    
    

    cout << "Welcome " << Fname <<" "<< Lname << " Employee of " << compname <<". What is the name of the store you will be buying from?" << endl; //Prints a welcome message with 3 vairables.
    cin >> storename; //Uses user-input to define storename variables.
    cout << "Thank you for shopping with " << storename << " Now, If you don't mind, How much will this transaction be?" << endl; //Prints a confirmation of storename and asks user to define tamount. 
    cin >> tamount; //Defines tamount with user input.
    cout << "You will be spending " << tamount << ". What is the date of transaction?" << endl; //Prints a confirmation of how much user will be spending, then asks them to define the date of transaction.
    cin >> dot; //Defines dot with user input.
    cout << "Date of tranaction is " << dot << '.' << endl; //Prints a confirmation of the date of transaction.


    if (tamount == 0) //Check to see if user passed tamount a string or 0.
    {
        cout << "You can not spend 0 dollors or you must enter a number, not letters." << endl; //Informs user they need a valid input.

    }

    else if((tamount > Maxbalance) || (tamount > cbalance)) //Checks if user passed tamount a variable greater then they can afford to spend.
    {
        cout << "You do not have enough for this transaction" << endl; //Informs user they can not afford this transaction.

    }
    else if ((tamount <= Maxbalance) && (tamount <= cbalance)) //Checks if user passed tamount a variable they can afford.
    {

        nbalance = cbalance - tamount; //Subtracts tamount from cbalance and passes its result into a new variable. 
    
        cout << "Transaction completed, you know have " << nbalance << " USD. Have a good day " << Fname << '.' << endl; //Informs user they can afford transaction and informs them of their new balance.

    }
    
    else //If something breaks, It should not, I tested it, but just in case it will output this. 
    {
        cout << "Im sorry, I don't understand what you said. Please restart this application" <<     endl;

    }
      
    return 0;

 }
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
Keth
  • 1
  • 1
    `cin >> storename;` will read `fff` (and leave the rest of input in the input buffer). `cin >> tamount;` will try to read a number but gets `fff` instead. So, the input fails (and all following as well). – Scheff's Cat Oct 09 '20 at 14:29
  • std::cin means charcter input, it might not support spaces – Build Succeeded Oct 09 '20 at 14:32
  • Please, try [google "site:stackoverflow.com c++ cin string"](https://www.google.com/search?q=site%3Astackoverflow.com+c%2B%2B+cin+string). (You are not the first one struggling with proper console input in C++.) There are lots of Q/As already there. (There are so many that I failed to find just the matching duplicate for your issue but I continue to try a bit...) – Scheff's Cat Oct 09 '20 at 14:33
  • You can use [`std::getline()`](http://www.cplusplus.com/reference/string/string/getline/) to read strings that contain whitespace characters. – MikeCAT Oct 09 '20 at 14:35

0 Answers0