Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2004
    Location
    Wisconsin
    Posts
    1,424

    this loop is confusing me....

    Ok, I'm still really new to C++.. I know VB pretty well however. I tried pre and post test for this loop, but neither ever work. Is it a syntax error? I looked through the loop with the debugger, and the newPerson char does change values, however the condition never evaluates to false..

    PHP Code:
        char newPerson 'y';
        do 
        {
            
    cout << "Enter person's name: ";
            
    cin >> guy.name;
            
    cout << "Enter person's age: ";
            
    cin >> guy.age;
            
    cout << "Enter person's gender: ";
            
    cin >> guy.gender;
            
    cout << endl;
                   
            
    cout << guy.name << "'s age is " << guy.age << ", and is " << guy.gender << endl;
                
            
    cout << "Would you like to change people? ";
            
    cin >> newPerson;

            
            }while (
    newPerson 'y'); 
    BTW, this isn't the entire program, the structure isn't defined here, but it's not relevant...

    Thanks!

  2. #2
    Join Date
    May 2003
    Location
    kangaroos
    Posts
    294
    while (newPerson = 'y');
    what you did is storing 'y' into newPerson.

    For comparision, it should be "=="
    while (newPerson == 'y');

  3. #3
    Join Date
    Dec 2004
    Location
    Wisconsin
    Posts
    1,424
    LOL... I hate learing little quirks of new languages.. I wonder if different languages will evolve into an "esperanto" type of language someday Thanks for the reply

  4. #4
    Join Date
    Jan 2005
    Location
    Surrey, UK
    Posts
    24
    I too know visual basic very well and it's the little things that really piss me off. E.g. "elseif" in vb and "else if" in c. I like the language from what i have learnt from it so far. Although i did like the way visual basic editor barged in and finished stuff for you.

  5. #5
    Join Date
    Apr 2004
    Location
    Saskatchewan, Canada
    Posts
    1,356

    Re: this loop is confusing me....

    Originally posted by redDC143C
    Ok, I'm still really new to C++.. I know VB pretty well however. I tried pre and post test for this loop, but neither ever work. Is it a syntax error? I looked through the loop with the debugger, and the newPerson char does change values, however the condition never evaluates to false..

    PHP Code:
        char newPerson 'y';
        do 
        {
            
    cout << "Enter person's name: ";
            
    cin >> guy.name;
            
    cout << "Enter person's age: ";
            
    cin >> guy.age;
            
    cout << "Enter person's gender: ";
            
    cin >> guy.gender;
            
    cout << endl;
                   
            
    cout << guy.name << "'s age is " << guy.age << ", and is " << guy.gender << endl;
                
            
    cout << "Would you like to change people? ";
            
    cin >> newPerson;

            
            }while (
    newPerson 'y'); 
    BTW, this isn't the entire program, the structure isn't defined here, but it's not relevant...

    Thanks!
    Thats really wierd that you didnt get a compiling error when tryign to do this. Even java would send out a compilation error because something went wrong. Also you might want to change it to this:

    while(newPerson == 'y' || newPerson == 'Y');

    I cant remember if c++ is case sensative or not, but if it is, you should have both the small and capital values accepted as true.

  6. #6
    Join Date
    Jan 2004
    Location
    South Pole
    Posts
    1,519
    Yes, but this is not Java. This is C++.

    And in C++, assignment operators return a value. Which I think is very convenient, because it lets you do things like "a=b=5;", which sets both a and b to 5.

    So allowing "if (a=b)" is not a bad thing, because it can be used. For example, suppose we have a function "f" that returns a pointer to something, but if it fails, it returns a NULL (which is zero).

    We could write it as:
    PHP Code:
    f();
    if (
    != NULL) {  //Which is the same as "if (p)"
        //Do something with p...

    But since assignment operators return the value assigned, it is more convenient to write it as:
    PHP Code:
    if (f()) {
        
    //Do something with p...


  7. #7
    Join Date
    Oct 2002
    Location
    Leicester, UK
    Posts
    3,335
    Originally posted by halma
    But since assignment operators return the value assigned, it is more convenient to write it as:
    PHP Code:
    if (f()) {
        
    //Do something with p...

    But some would argue that it's less readable especially if the function name is longer and accepts parameters.
    Forget the username, call me Keith

  8. #8
    Join Date
    Jan 2004
    Location
    South Pole
    Posts
    1,519
    It looks cool, though!

    I don't find it that confusing. After you get used to it, it rarely causes a bug. I've also had bugs the other way, for example by writing "a==5;" instead of "a=5;"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •