-1

Good day,

I was writing this code for a school project, but after compiling and executing it, after hitting yes, I was unable to enter a name. I am not sure why this is happening, happened to both codeblocks and vs community. I've tried adding cin.ignore(); below getline(cin, customer) and it kinda works but I am not able to enter the name of a business. If I put cin.ignore() on both, the enter number field will be skipped and it will go directly to the results. I will attach an image below (without using cin.ignore)enter image description here

       #include "pch.h"
       #include <iostream>
       #include <string>
       #include <cstdlib>
       #include <ctime>

       using namespace std;

       int main()
{

string input;
cout << "hello, press yes to start or no to cancel \n";
cin >> input;

if (input == "no")
{
    cout << "have a nice day \n";
}
else if (input == "yes")
{
    string customer;
    string address;
    int user_enter_number;
    const double pc = 0.05 * 10;
    double amount, abacus, price; // abacus = AMOUNT
    time_t now; // rawtime
    struct tm nowlocal;


    cout << "Please enter your name " << endl;
    getline(cin, customer);   ` when executing, I was not able to type my 
                               name and instead was skipped and went to 
                               enter the store's name.`

    cout << "Please enter the store's name \n"; 
    getline(cin, address);

    cout << "Please enter the number you would like to buy, the range is 
    from 00 - 99 \n";
    cin >> user_enter_number;

    cout << "Please enter the amount you wish to spend ($): \n";
    cin >> amount;

    abacus = amount * pc;
    price = amount;

    now = time(NULL); // obtains the time from the operating system
    nowlocal = *localtime(&now);

    cout << customer << " you have bought this number: " << 
    user_enter_number << endl;
    cout << "you have bought this pcs: " << amount / 0.05 << endl;
    cout << "this ticket was bought on: " << nowlocal.tm_mday << "/" << 
    nowlocal.tm_mon + 1 << "/" << nowlocal.tm_year + 1900 << endl;
    cout << "at this time: " << nowlocal.tm_hour << ":" << nowlocal.tm_min << ":" << nowlocal.tm_sec << endl;

    srand(time(NULL));

    int min = 00;
    int max = 99;
    int num = (min + (rand() % (int)(max - min + 1)));

    cout << "the winning number is: " << num << endl;

    if (num == user_enter_number)
    {
        cout << customer << " You have won" << amount << "Please visit your 
         nearest store to collect! \n";
    }
    else
    {
        cout << "Try again next time! \n";
    }

     return 0;
}    
skyhigh
  • 11
  • 4
  • I opened your link, but the info provided does not fully solve my problem. Thank you still for helping me! – skyhigh Sep 23 '18 at 00:05
  • You should read and understand the answer to the duplicate. The solution presented there is the same as the one given in dstackflow's answer here. – Miles Budnek Sep 23 '18 at 00:15
  • Your screen snapshot of the output is difficult to read. Please edit your post with the *text* of the output. – Thomas Matthews Sep 23 '18 at 00:39
  • I’m sorry, I just wanted to snap shot the results as to show the exact problem. Thankfully the problem has been solved! – skyhigh Sep 23 '18 at 00:41

1 Answers1

0

The first line of input is a line. Use getline for that too.

cin >> input;

Leaves the newline in the buffer.

getline(cin, customer);

Only gets to read the newline from the first input.

Captain Giraffe
  • 13,403
  • 5
  • 35
  • 65