Program 16

Pages: 123
Sep 1, 2014 at 12:03pm
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 Sep 16, 2014 at 12:26pm
Sep 1, 2014 at 12:09pm
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 Sep 1, 2014 at 12:11pm
Sep 1, 2014 at 2:19pm
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.
Sep 1, 2014 at 4:13pm
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.
Sep 1, 2014 at 5:12pm
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 Sep 15, 2014 at 7:50am
Sep 1, 2014 at 5:17pm
Did you really mean this:

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

?

Did you actually read my description of what this line does?
Sep 2, 2014 at 12:24pm
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 Sep 15, 2014 at 7:50am
Sep 2, 2014 at 1:43pm
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.
Sep 2, 2014 at 5:48pm
Well, I`m not sure what I should use then. Is it otherwisely correct? The terminating | was also a bit confusing.
Last edited on Sep 2, 2014 at 5:48pm
Sep 2, 2014 at 5:59pm
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.
Sep 2, 2014 at 6:31pm
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 Sep 2, 2014 at 6:35pm
Sep 2, 2014 at 7:42pm
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.
Sep 3, 2014 at 6:32am
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 ^^
Sep 3, 2014 at 10:57am
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


}
Sep 3, 2014 at 11:24am
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.
Sep 3, 2014 at 12:14pm


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 {}
   
  
}
Sep 3, 2014 at 12:43pm
char '|'
cin>> '|'

ifstream |;

if (|) return 0;

else {}


This makes zero sense.

Aceix.
Sep 3, 2014 at 1:17pm
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.
Sep 3, 2014 at 3:42pm
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 Sep 3, 2014 at 3:43pm
Sep 3, 2014 at 4:39pm
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