Program 16

Pages: 123


"You need to go back and re-learn the basics. "


Exactly so, but is this not my problem? I believe everyone has their own ways of learning and I believe that right answers are part of the solution for me. The basics of C/C++ are important and one day I learn them but now I have this concept seems unintresting.

As soon as I will have more time/interest I will revise the basics but now lacking of time, but try to keep up doing some exercises,

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 #include<iostream>

using namespace std;

int main()

{

int a=0;
int b=0;

while  (a<100 || b<100) {

cout<<" Enter two integer values ";
cin>>a>>b;
cout<<a<<b ; }

char c;                                    
c= {' '}                                         // Set a default value to c
                            
cout<< "To quit the program, enter |";
cin>> 'c'; 


if ( 'c' ==  '|') {

return 0; }

else {}


}
It's unreasonable to expect people on this site to type up rudimentary information that you can easily find in the introductory sections of a tutorial or textbook, just because you find that "unintresting".

On a more pragmatic note, if you'd taken my advice, you could have understood what you're doing wrong with about 30 mins of reading to refresh your knowledge - rather than the days that this thread has run for so far.
As soon as I will have more time/interest I will revise the basics but now lacking of time, but try to keep up doing some exercises

An the only thing that you do need in doing the exercises is knowledge of the basics.

I bet there is a meme about this.

It is true that there are personal differences on the uptake, but there are differences in topics as well. For example, syntax errors in exercise program are usually less fatal than untrained handling of Ebola samples.
Sorry for long answer but I`m still somewhat lost here even though I did some research.


Should I use the boolean function instead to test for whether an error flag on the stream has been set? This way I can have the compiler read as long as the test is true.


something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

char c;
c= '|';
bool test = true;
while(test)

cin>>c;

if  (cin.fail())

{ cout<< "Leave loop";

test= false;  }

 else { return 0;
}    
 


Last edited on
Compiler does not read input; program does.

Think about:
1
2
3
4
5
char foo;
while ( std::cin >> foo )
{
  // do something with foo
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

 #include<iostream>

using namespace std;

int main()

{

int a=0;
int b=0;

while  (a<100 || b<100) {

cout<< "Enter two integer values (| to quit): <<\n";
cin>>a>>b;
cout<<a<<b; }

char c;
while ( cin>>c )
{
  
if ( c ==  '|') {

return 0; }

else {}

}
}


But now the whole program goes crazy
Last edited on
Your indentation is not informative. Reformat of your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
  int a=0;
  int b=0;

  while  ( a < 100 || b < 100 ) // This loop will continue until both a and b are >=100
    {
      cout<< "Enter two integer values (| to quit): <<\n";
      cin >> a >> b; // If you do type a non-number, then cin is in error state and a and b no longer change
      cout << a << b;
    }

  char c;
  while ( cin >> c )
    {
      if ( c ==  '|')
        {
          return 0;
        }
      else
        {
        }
    }
}
It`s not working for me for some reason. Have you tested that it works?
But now the whole program goes crazy
It`s not working for me for some reason.

Is there any reason why you're being vague? This would be easier if you'd explain clearly in what way it's not working.

Have you tested that it works?

Why should keskiverto test it? It's your code! keskiverto's just reformatted the whitespace to make it more readable. Oh, and added some helpful comments which should make it clearer as to why it's not behaving the way you want it to.
Last edited on
Ok, I wasn`t clear enough, when I enter the | character something mystical happens (copypaste it because it is not on my keyboard) somethig that one wouldn`t expect. It does not blow up my keyboard or computer but still one is inclined to think that it shouldn`t happen if the program worked correctly. And I`m convinced of that.
Last edited on
Ok, I wasn`t clear enough, when I enter the | character something mystical happens


Oh that's a lot clearer :)
Add cout << "Hello world\n"; between (line 18 in your code) the two while-loops.
Run the program.
Enter the | like before.
Did you enter the character before or after the "Hello" has printed?
Last edited on
If I change return 0; to cout << "Hello world\n"; I`m facing the same error as before(terminal window getting mixed) and it doesn`t even print the words " Hello world" at all
Not the return 0.
Just before
char c;
Hello does not print out at all.
Yes. That was expected.

Input two numbers that are over 100. The you should see Hello, and after that it is safe to type non-integers, such as |.
What if I want that both of the numbers are smaller than 100, then I should use something like

while ( a < 100 && b < 100 ) ?

Anyway the program is not still working like was expected because if I enter the numbers that are over 100

then I just see the text 200201Hello world and if I enter | after entering the numbers the program doesn`t quit

to prevent that from happening I should somehow connect the two loops?
Last edited on
1
2
3
4
int a, b;
while ( std::cin >> a >> b ) {
  std::cout << a << '\t' << b << '\n';
}

Now it works, my current code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 #include<iostream>

using namespace std;

int main()
{

  cout<< "Enter two integer values (| to quit): <<\n";
  
  int a=0;
  int b=0;

  while  ( cin>>a>>b ) 
    {
      
      cout << a << '\t' << b << '\n';
      
    }

  cout << "Hello world\n";

  char c;
  while ( cin >> c )
    {
      if ( c ==  '|')
        {
          return 0;
        }
      else
        {
        }
    }
}


Thank you for help anyway
What happens if the very first character a user enters is '|'. Does the program behave as you intended?
Pages: 123