Terminate input on '|'

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Terminate input on '|'

Post by cplusplusnoob »

I am try to do a program that get's 2 ints from the user and outputs them to the user using a while statement and having the capacity to terminate by entering | by the compiler i'm pretty sure my while statement is messed up although it looks anatomicly cerrect

Code: Select all

[{
	using namespace std;
	int number_1;
	int number_2;
	cout<<"put in your first number.\n";
	cin>>number_1;
	cout<<"Now enter your second number.To exit the program enter |\n";
	cin>>number_2;
	while(number_1||number_2!="|"){
		cout<<"number_1<<"  "<<number_2<<"     \n";
	}/code] 


and thank you all for your time. ;)
I'm not modest, I'm brutally honest.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Terminate input on '|'

Post by Ginto8 »

#1. Your while loop will simply out put the first and second number infinitely. There is no chance for the user to input a |.

#2. The conditions for your while loop are wrong. It should be this:

Code: Select all

while(number_1 != '|' && number_2 != '|')
if you had number_1 || number_2 != '|', it will continue so long as either number_1 does not equal 0, or number_2 does not equal '|'. Also, you can't have "|". This will create 2 characters; '|' and the character used to terminate strings, '\0'. this will make it so that it is not looked at the way you want it to.

#3. Try to correctly use the [ code ] brackets =P ;) :mrgreen: :lol:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: Terminate input on '|'

Post by cplusplusnoob »

Ginto8 wrote:#1. Your while loop will simply out put the first and second number infinitely. There is no chance for the user to input a |.

#2. The conditions for your while loop are wrong. It should be this:

Code: Select all

while(number_1 != '|' && number_2 != '|')
if you had number_1 || number_2 != '|', it will continue so long as either number_1 does not equal 0, or number_2 does not equal '|'. Also, you can't have "|". This will create 2 characters; '|' and the character used to terminate strings, '\0'. this will make it so that it is not looked at the way you want it to.

#3. Try to correctly use the [ code ] brackets =P ;) :mrgreen: :lol:
i appreciate your help so much i change my while loop like your's to be cerrect and got rid of my " before number_1 and it will ask for the numbers but wont outbut anything.i don't understand why for extra refence i'll post my new code.

Code: Select all

{
	using namespace std;
	int number_1;
	int number_2;
	cout<<"put in your first number.\n";
	cin>>number_1;
	cout<<"Now enter your second number.To exit the program enter |\n";
	cin>>number_2;
	while(number_1 =!'|' &&number_2 !='|'){
		cout<<number_1<<"  "<<number_2<<"     \n";
	}
I'm not modest, I'm brutally honest.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Terminate input on '|'

Post by wtetzner »

You're trying to store a character '|' in an integer. So cin will fail if the user enters '|'. Also, in your while loop, when you compare an integer and a character, it compares the integer against the ascii value of the character.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: Terminate input on '|'

Post by cplusplusnoob »

wtetzner wrote:You're trying to store a character '|' in an integer. So cin will fail if the user enters '|'. Also, in your while loop, when you compare an integer and a character, it compares the integer against the ascii value of the character.
you are right ill try to fix it.
I'm not modest, I'm brutally honest.
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: Terminate input on '|'

Post by cplusplusnoob »

cplusplusnoob wrote:
wtetzner wrote:You're trying to store a character '|' in an integer. So cin will fail if the user enters '|'. Also, in your while loop, when you compare an integer and a character, it compares the integer against the ascii value of the character.
you are right ill try to fix it.
But how??i dont know of any possiblle conversians other than the ascii one that allow char to int hmm how???
I'm not modest, I'm brutally honest.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Terminate input on '|'

Post by programmerinprogress »

I don't really have the time to test this theory, but i'm going to explain how you could do it (as I see it mentally)

you could get a string of characters, input your number into the string, the string acts as a buffer.

you then use some bitmasks to reduce each character code to a simple digit (so ASCII 48 ('0') becomes 0, 49 becomes 1 etc ), then you 'multiply up' each digit and then add it to an integer value.

I couldn't tell you which bitmask to use, or how the multiplication algorithm would be written, as i'm a little busy now, but if you could come up with a solution using this principle, I would be very interested to find out if that worked.

PS: i'm not lazy and I will try it out when I have the time ;) .
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: Terminate input on '|'

Post by cplusplusnoob »

programmerinprogress wrote:I don't really have the time to test this theory, but i'm going to explain how you could do it (as I see it mentally)

you could get a string of characters, input your number into the string, the string acts as a buffer.

you then use some bitmasks to reduce each character code to a simple digit (so ASCII 48 ('0') becomes 0, 49 becomes 1 etc ), then you 'multiply up' each digit and then add it to an integer value.

I couldn't tell you which bitmask to use, or how the multiplication algorithm would be written, as i'm a little busy now, but if you could come up with a solution using this principle, I would be very interested to find out if that worked.

PS: i'm not lazy and I will try it out when I have the time ;) .
This makes some sence to me but im only on page 123 of 1236 on my book so i got a lot to learn but what im really asking is is there a simpler way to do this?I wish i could test this but like i said i dont have the expertiese.
I'm not modest, I'm brutally honest.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Terminate input on '|'

Post by programmerinprogress »

theres probably multiple ways of doing this, i'm just a bitwise nut :lol:

Bitwise operators and Bitmasking is pretty simple once you get the hang of it, I actually learnt it in my computing class, not from a book (I actually learned the theory in class, then learnt how to apply the logic to C++ from the book)

this subject has intrigued me, and i'm only having a few simple problems that are stopping me from solving this one (once I solve it, i'll post it, and relay it to you with some explaination)

My problem is, I see char's as numbers and codes in my head, so changing those numbers to their integer counterparts just seems logical to me.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: Terminate input on '|'

Post by cplusplusnoob »

ok just don't forget im at the end of the chapter so ill just skip that one for now and do some of the other execises.
I'm not modest, I'm brutally honest.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Terminate input on '|'

Post by programmerinprogress »

never say never cplusplusnoob :lol:

As a side note, I actually got the think to work, but it's a little clunky, and I need to refine the technique a little, but I just wanted to prove the concept.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: Terminate input on '|'

Post by cplusplusnoob »

thanx for the encouragement every bit helps. ;)
I'm not modest, I'm brutally honest.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Terminate input on '|'

Post by wearymemory »

First of all, you don't even tell the user that they could exit the program by entering | when you first ask for the user's input, yet you still check to see if both numbers contain |.

And secondly, why the hell do you need a while loop for that? It doesn't seem logical to do that, unless of course this was assigned to you and you absolutely have to do it this way.

This may not be exactly what you want, but it offers somewhat of a solution to your problem. The overall structure of your program confused me, so this code was my best attempt at imitating it.

Code: Select all

#include <iostream>
using namespace std;

int main ()
{
    int input;
    string str;

    while(1) {

        cout << "Enter a number (| to quit): ";
        cin >> input;

        if(cin.fail()) {
            cin.clear();
            cin >> str;

            if(str == "|") {
                break;
            }
        } else {
            cout << "You entered: " << input << endl;
        }

    }

    return 0;
}
Last edited by wearymemory on Mon Mar 16, 2009 11:37 am, edited 1 time in total.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Terminate input on '|'

Post by programmerinprogress »

that worked very nicely ;)

a simple solution to a simple problem, funnily enough, i've never been able to get cin.clear() to work in the past, it's always just ignored my command to stop, therefore i've been trying to find a solution that worked around that.

But yours worked perfectly fine, so now theres no need whatsoever :)

also, a very clean loop was used, I may have been tempted to write a Do...while(str != "|"), but that's just a matter of preference (really just to eliminate an If statement from the mix)

here's how I would write this, now that my cin.clear() appears to work

Code: Select all

#include <iostream>
using namespace std;

int main() {

    int input;
    string str;

    do{

        cout << "Enter a number (| to quit): ";
        cin >> input;

        if(cin.fail()) {
            cin.clear();
            cin >> str;
            cout << "That was not a number" << endl;


        }
        else
        {
            cout << "You entered: " << input << endl;
        }



    }while(str != "|");

    return 0;
}

---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: Terminate input on '|'

Post by cplusplusnoob »

wearymemory wrote:First of all, you don't even tell the user that they could exit the program by entering | when you first ask for the user's input, yet you still check to see if both numbers contain |.

And secondly, why the hell do you need a while loop for that? It doesn't seem logical to do that, unless of course this was assigned to you and you absolutely have to do it this way.

This may not be exactly what you want, but it offers somewhat of a solution to your problem. The overall structure of your program confused me, so this code was my best attempt at imitating it.

Code: Select all

#include <iostream>
using namespace std;

int main ()
{
    int input;
    string str;

    while(1) {

        cout << "Enter a number (| to quit): ";
        cin >> input;

        if(cin.fail()) {
            cin.clear();
            cin >> str;

            if(str == "|") {
                break;
            }
        } else {
            cout << "You entered: " << input << endl;
        }

    }

    return 0;
}
thank you i will be sure to try it and i have the utmost confidance in it your help is appreciated and in my defence in the book im reading i didnt know some of the things you used but at any rate thank you for your time and support.
I'm not modest, I'm brutally honest.
Post Reply