user5590284
user5590284

Reputation:

Exit while loop once input has ended

I am writing a program and in one part of the program I need to read in integer inputs from user and multiply each number by the mod of two other numbers and then convert that number to its ASCII character value and output it to a file. But all of that works fine... I just cannot get the loop to end. Every time I enter some integers, for example 67 78 100, it will convert each number and output it to the file like its suppose to but instead of continuing with the rest of the program once its done it goes back to the top of the loop and asks the user for another input. So how can I get the loop to end after it has converted all the numbers and stored them in a file.

Relevant code...

while(cin >> f && f != '\n')
{
    num = f * b % N;
    CharFile << char(num) << endl;
}

Upvotes: 1

Views: 54

Answers (2)

Kon
Kon

Reputation: 4099

cin is meant for formatted text, meaning it will not give you things like newline characters. To get unformatted character, try cin.get() to see if you're reading newline:

while(cin >> f)
{
    num = f * b % N;
    CharFile << char(num) << endl;
    if (cin.get() == '\n') break;
}

Upvotes: 1

Alireza Sattari
Alireza Sattari

Reputation: 185

use break;. For more information, check this website out: https://www.tutorialspoint.com/cplusplus/cpp_break_statement.htm

Upvotes: 0

Related Questions