Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this code involving cin skip getline even with cin.ignore()?
    primarykey
    data
    text
    <p>The following code works as intended for the first two getlines and after you input the CC variable it goes into an infinite loop skipping the getlines and not waiting for input.</p> <p>Here is a sample run:</p> <p>Enter card holder name (or quit): John Doe</p> <p>Enter CC number: 1234 1234 1234 1555</p> <p>// code outputs the other couts but does not wait for input of getline. and reiterates the cout statements. cin.ignore does not seem to help or cin.clear()</p> <p>Code:</p> <pre><code> int main(int argc, char* argv[]) { char CCName[64]; //cardholder name char CCNumber[16]; //credit card number char Expiration[8]; //expiration date float Amount; while (true) { /* input processing block */ //gather card holder name cout &lt;&lt; "\nEnter card holder name (or quit): "; cin.getline(CCName, 64); //quit command processing if (strcmp(CCName, "quit") == 1) { cout &lt;&lt; "\nYou successfully terminated the program\n"; //~ close(sockfd); //close socket exit(EXIT_SUCCESS); } //gather credit card number cout &lt;&lt; "\nEnter CC number: "; cin.getline(CCNumber, 16); //error checking if (strlen(CCNumber) != 15 &amp;&amp; strlen(CCNumber) != 16) { cout &lt;&lt; "\nCredit card number must be 15 to 16 digits, try again: "; cin.getline(CCNumber,16); } //gather expiration date cout &lt;&lt; "\nEnter expiration: "; cin.ignore(); cin.getline(Expiration, 7); //error checking if (strlen(Expiration) != 7) { cout &lt;&lt; "\nExpiration date format mm/yyyy. Try again: "; cin.getline(Expiration, 7); } //gather amount cout &lt;&lt; "\nEnter amount: "; cin &gt;&gt; Amount; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload