-6
  1. 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.

How do you write this?

When I check to see if either int is equal to | they never do because their value is zero if | is entered. My program currently repeats forever if | is entered. I don't know how to access the correct value of the non int | by using ints nor how to stop it from repeating forever.

1 - #include <iostream>
2 - 
3 - using namespace std;
4 - 
5 - int main()
6 - {
7 -     int val1=0, val2=0;
8 -     while(val1 != '|' && val2 != '|')
9 -     {
10-        cout << "Enter 2 integers, terminate with \"|\"" << endl;
11-        cin >> val1;
12-        if (val1 == '|')
13-        {
14-            return 0;
15-        }
16-        cin >> val2;
17-        if (val2 == '|')
18-        {
19-            return 0;
20-        }
21-        cout << val1 << " " << val2 << "\n\n";
22-    }
23-
24-    return 0;
25- }
Shadow
  • 351
  • 2
  • 5
  • 15
  • 2
    rather then just putting -1 without saying anything, you could try to help – Shadow Dec 29 '14 at 23:04
  • 2
    A downvote means your question is unlikely to be useful to anyone in the future. This is not a tutorial site, or site to solve homework problems. Its goal is to accumulate *programming* problems and solutions, general enough to be helpful to many people. Use a debugger on your program and the issue will become apparent instantly. – BartoszKP Dec 29 '14 at 23:19
  • 1
    1. Try changing the title to actually reflect the problem. No one cares or wants/needs to know if it's homework. 2. Read the faq about how to post a question. – UpAndAdam Dec 29 '14 at 23:23
  • Thank you both for actually saying what's on your mind. I can actually learn a lot more from this than the -1's by themselves. – Shadow Dec 29 '14 at 23:26

4 Answers4

1

You have two problems. First you aren't "priming the pump" and second you are trying to convert a pipe to an int so that's going to be a problem. Rather than casting val1 and val2 as ints at the beginning, type them as strings. Since you are simply printing the values and not doing any math, it shouldn't matter. From there you need to read your input once before you drop into the while statement. You do this so if you immediately get a pipe the program will stop.

int main()
{
    string val1=0, val2=0;
    //grab the values first, before checking at the head of the loop
    cin >> val1;
    cin >> val2;

    while(val1 != '|' && val2 != '|')
    {
       //You may want to check for empties in your while as well
       cout << "Enter 2 integers, terminate with \"|\"" << endl;
       cout << val1 << " " << val2 << "\n\n";           
       //recharge val1 and val2 before looping next time.
       cin >> val1;
       cin >> val2;
   }

    return 0;
 }
unixcorn
  • 21
  • 1
0

Instead of reading val1 and val2 directly from stdin, read a line of text. If the line does not start with |, extract the numbers from the line using sscanf or istringstream. If the line starts with |, break out of the while loop.

#include <stdio.h>
#include <iostream>

using namespace std;

const int LINE_SIZE = 200; // Make it large enough.

int main()
{
   char line[LINE_SIZE] = {0};
   int val1=0, val2=0;

   // Break out of the loop after reading a line and the first character
   // of the line is '|'.
   while( true )
   {
      cout << "Enter 2 integers, teminate with \"|\"" << endl;

      // Read the entered data as a line of text.
      cin.getline(line, LINE_SIZE);
      if ( !cin )
      {
         // Deal with error condition.
         break;
      }

      // If the first character of the line is '|', break out of the loop.
      if ( line[0] == '|' )
      {
         break;
      }

      // Read the numbers from the line of text.
      int n = sscanf(line, "%d %d", &val1, &val2);
      if ( n != 2 )
      {
         // Deal with error condition.
         continue;
      }

      cout << val1 << " " << val2 << "\n\n";
   }

   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You can use istream.peek() to tell what the next character is without consuming it:

int main()
{
    int val1 = 0;
    int val2 = 0;
    while (std::cin.peek() != '|')
    {
       std::cin >> val1;
       std::cin >> val2;
       std::cout << val1 << " " << val2 << std::endl;
   }

   return 0;
}

It is also a good practice to check for EOF:

while (std::cin.peek() != EOF && std::cin.peek() != '|')

And you don't have to use two variables, put them inside the scope:

int main()
{
    while (std::cin.peek() != '|')
    {
       int val = 0;
       std::cin >> val;
       std::cout << val << " ";
       std::cin >> val;
       std::cout << val2 << std::endl;
   }

   return 0;
}

Although this won't work if you need to skip white characters, like '\n'. You should try to see this related question to work around this problem: How to properly use cin.peek()

Community
  • 1
  • 1
Antoine Pietri
  • 793
  • 1
  • 10
  • 25
0

Please follow comments in the code

// while (cin >> variable), read whitespace-separated values in type condition.

// We use character '|' to terminate the input — anything that isn’t an int type can be used.

// for string type, Ctrl+Z terminates an input stream under Windows and Ctrl+D does that under Unix.

    int main ()
    {
        int val1;

        int val2;

        cout << "Enter 2 integers: \n";

        while (cin >> val1 >> val2 )

             cout << "You entered: \n" << val1 <<'\n' << val2 << '\n';

        return 0;
    }
Adam
  • 2,726
  • 1
  • 9
  • 22
Khaled Eid
  • 54
  • 5