Program 16

Pages: 123
Hello,

I have written a code that is probably filled with errors but I haven`t had much time lately looking to C++ so that could be expected.

Here is the description:

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

Here is my version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include<iostream>

int main()

{

int a=0;
int b=0;
cout<<" Enter two integer values ";
cin>>a>>b;

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

cout<< "a" && "b" ;

cout<< "To quit the program, enter |";
cin>>|;
terminate(); 

}
Last edited on
you aren't doing anything in your while loop.

asking the user, testing the input, outputting to the screen ALL has to be done inside your loop.
Last edited on
The use of terminate() here is not advisable. Use return instead.
Also, the syntax of while loops is
while(/*condition*/){/*statement to execute if condition is met*/}

Aceix.
mutexe wrote:
you aren't doing anything in your while loop.

... except writing to nstandard output the result of evaluating "a" && "b". "a" and "b" are both string literals, so evaluate as non-zero pointers. "a" && "b" will therefore always evaluate to true.

This means that whenever a or b is less than 100, you'll get an infinite loop which repeatedly writes "1" to stdout.
Yes I got lost. Here is my code improved, sorry for not better quality, getting many errors when trying to compile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include<iostream>

int main()

{

int a=0;
int b=0;

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

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

cout<< "To quit the program, enter |";
return 0; 

}





Last edited on
Did you really mean this:

cout<< "a" && "b" ;

?

Did you actually read my description of what this line does?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #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" and "b" ;

cout<< "To quit the program, enter |";
cin.get();
return 0; 

}

}


now it compiles but does not follow the original description.
Last edited on
cout<< "a" and "b" ;

The C++ operator and is synonymous with the operator &&.

In other words, it's still doing exactly the same thing as the previous version, which I explained to you earlier in the thread.
Well, I`m not sure what I should use then. Is it otherwisely correct? The terminating | was also a bit confusing.
Last edited on
You know how to stream more than one item from an istream. Hint: streaming more than one item into an ostream is very similar.

Your program will only ever perform one pass through the loop, since you have return 0; within your loop.
I fixed the line 15 to cout<<a<<b.

2) I can`t associate entering | to return 0;

Maybe use an if-statement?
Last edited on
Yes, exactly.

Forget about the code for a moment, and just think about it logically. You want to read the user's input, and then make a decision based on what the user enters.

Making a conditional decision usually means using an if statement.
You're code saying that your while loop is a mess but not really. You did just put a while loop but did not have a statement ^^
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
 #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 ; }

cout<< "To quit the program, enter |";

if (cin>>|)  return 0;

else {}                                   // Do nothing


}
A couple of points:

1) The symbol | on its own is the bitwise OR operator. If you want to treat it as a character, you need to surround it in single quotes, the way you would any other character. If you want to treat it as a string containing a single character, you need to surround it with double-quotes, like any other string.

Does that line even compile?

2) Even if you treated it as a string, if (cin>>"|") wouldn't do what you seem to think it does. If you look at the reference for the >> operator for the istream class:

http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

you'll see that the return value is the istream object itself. ANd if you look at the reference for the overloaded bool operator for the class:

http://www.cplusplus.com/reference/ios/ios/operator_bool/

you'll see that it tests for whether an error flag on the stream has been set.

What you want to do is:

- use cin to read the user input into a variable
- test the value of the variable to see whether the user typed a | character.

I strongly recommend you go back to your textbook and try and get a better understanding of how to use I/O streams.


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>
#include <fstream>
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 ; }

cout<< "To quit the program, enter |";

char '|'
cin>> '|'

ifstream |;

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

ifstream |;

if (|) return 0;

else {}


This makes zero sense.

Aceix.
MikeyBoy wrote:
I strongly recommend you go back to your textbook and try and get a better understanding of how to use I/O streams.


OK, I'm going to have to revise that. I don't mean to sound harsh, but I think you need to go right back to the fundamentals of C/C++ programming, so that you understand what a variable is, how you define them, how you assign values to them, etc.

Lines like:

1
2
3
4
5
6
char '|'
cin>> '|'

ifstream |;

if (|) return 0;


are simply meaningless - they're not C or C++, and they have no meaning. It honestly looks like you're just typing anything you can think of, and hoping to magically stumble across a combination of characters that works. You will never, ever, be able to successfully program like that.

I know you've said that you haven't had much time lately to practice programming, but there's not really any way around it: you need to go back to the basics, and re-learn them.
Should be something 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
#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 ; }

cout<< "To quit the program, enter |";

char |= 'a'
cin>> a                         // a is a variable


if ( a ==  '|')

return 0;

else {}


}
Last edited on
20
21
char |= 'a'
cin>> a                         // a is a variable 

I can only re-iterate my previous comments. If you can't even remember how to declare a simple character variable, you need to go back and re-learn the basics.
Pages: 123