-4

I'm going through Bjarne Stroustrup's Principle's and Practices using C++ and I'm stuck on one specific exercise. The exact question is:

"Write a program that consists of a while-loop that (Each time around the loop) reads in two ints and then prints them. Exit the program when a terminating '|' is entered."

I thought the code below would work fine, but I keep getting a never ending loop.

#include <iostream>

int main() {
  int i1 = 0;
  int i2 = 0;

  while ((i1 != '|') || (i2 != '|')) {
    std::cout << "Enter some nums YO (enter | to exit)\n";
    std::cin >> i1 >> i2;
    std::cout << i1 << i2;
  }

}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

4 Answers4

1
std::cin >> i1 >> i2;

Tells the program to read two integers, so the program looks in the stream for numbers. If you input '|', there is no number to be found and the stream is put into a failure state. The failure state will need to be cleared and the remainder of the bad input ignoreed before more input can be accepted. But if you put in the numeric encoding for the character '|' (124 assuming ASCII encoding), the stream input and the conditional will both be satisfied, but now it's impossible to input the number 124 and mean 124 and not '|'.

Instead read in std::strings. If neither of the strings are "|", test that the strings really are integers (Probably by trying to convert them to integers with std::stoi). If you find "|", exit the loop.

MSalter's comment shook loose another solution that is much simpler than going to strings. If 2 numbers were read, print the numbers and ask for more. Otherwise, clear the error from reading a non-integer and read in a char to test for '|'. Here is a really simple and ugly cut:

#include <iostream>

int main()
{
    int i1 = 0;
    int i2 = 0;
    char ch = ' '; // can be anything but |
    do
    {
        std::cout << "Enter some nums YO (enter | to exit)\n";
        if (std::cin >> i1 >> i2) 
        { // we got nums, YO.
            std::cout << i1 << i2; // print the nums, YO
        }
        else
        { // not nums, YO. Let's see if we got a |
            std::cin.clear(); // clear the error
            std::cin >> ch; //read for a '|'
        }
    } while (ch != '|' && std::cin); // loop until | is read or cin can't read a char
}

It's ugly because input like "auoaioafksdjnfkm" will print out a prompt for each character and "klasdjfahs12938 2093854sjfljzlkfjzlk" will print out excessive prompts and a "129382093854" somewhere in the middle.

user4581301
  • 33,082
  • 7
  • 33
  • 54
0

int stores a number. '|' is a character, then use char to read/store it.

char i1;
char i2;

example:

int main() {

    char i1;
    char i2;

    while ((i1 != '|') || (i2 != '|')) {
        std::cout << "Enter some CHARACTERS (enter '|' to exit)\n";
        std::cin >> i1 >> i2;
        std::cout << i1 << i2;
    }
}
Ivan Sanz Carasa
  • 1,141
  • 8
  • 16
0

the loop keeps going on because you are making a comparison betwen an integer variable and a character value

while(i1!='|'||i2!='|)//does not work

because i1 and i2 can only posses values of numbers(0123456789) and not charactes(a,b,c,!,ª,|) i1 and i2 can never take the value '|' and the loop runs over and over. one thing that should work:

    int main() {
  int i1 = 0;
  int i2 = 0;
  char c;//declare c
  while (c!='|') {//if the value of c is not equal to '|' run the loop
    cout << "Enter some 2 nums and a character YO (enter | to exit)\n";
    cin >> i1 >> i2 >> c;//give a value to i1,i2 & c
    cout << i1 << i2;
}

}
0

Read a line of text from the console. Exit if the line contains a |. Otherwise, parse the line.

int main()
{
    for (;;)
    {
        std::cout << "Enter some nums YO (enter | to exit)" << std::endl;
        std::string s;
        std::getline(cin, s);
        if (s.find('|') != s.npos)
        {
            break;
        }
        std::istringstream ss(s);
        int i1, i2;
        ss >> i1 >> i2;
        std::cout << i1 << ' ' << i2 << std::endl;
    }
    return 0;
}
Sid S
  • 6,037
  • 2
  • 18
  • 24