CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Question Function problem

    I am trying to make a function that repeats till I say exit out. For some strange reason it's complaining about the answers.

    Code:
    Code:
    int add()
    {
            char answer;
            int a,b;
            int total;
    
            do
            {
                    std::cout<<"Enter two numbers: ";
                    std::cin>>a,b;
    
                    total = a+b;
                    std::cout<< total <<"\n\n";
                    std::cout<<"Do you wish to continue: Y/N";
                    std::cin>>answer;
            } while (answer = y || answer = Y);
    
            std::cout<<"Returning to menu\n\n";
            return 0;
    }
    Error:

    Code:
    In file included from index.cpp:2:0:
    functions.cpp: In function &#226;int add()&#226;:
    functions.cpp:9:11: error: &#226;y&#226; was not declared in this scope
    functions.cpp:20:34: error: &#226;Y&#226; was not declared in this scope
    Last edited by fuzzylr; May 3rd, 2011 at 03:50 PM.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Function problem

    90 posts is enough to know about code tags.

    Look at this line. What do you see wrong?

    while (answer = y || answer = Y);

  3. #3
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Re: Function problem

    Quote Originally Posted by GCDEF View Post
    90 posts is enough to know about code tags.

    Look at this line. What do you see wrong?

    while (answer = y || answer = Y);
    Actually, I thought I had them right. However, it's not working. I even googled it up on V board.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  4. #4
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Re: Function problem

    Quote Originally Posted by GCDEF View Post
    90 posts is enough to know about code tags.

    Look at this line. What do you see wrong?

    while (answer = y || answer = Y);
    Sorry, I had the "/" in the wrong direction. I actually did use tags.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  5. #5
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Re: Function problem

    Quote Originally Posted by GCDEF View Post

    Look at this line. What do you see wrong?

    while (answer = y || answer = Y);
    I'm not sure honestly. It's been a long time since I've done programming. I've probably made a silly mistake. I've tried several different variants.

    Code:
    while (answer == y | answer == Y);
    Code:
    while ((answer = y) | (answer == Y));
    Code:
    while (answer == "y" | answer == "Y");
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Function problem

    = is assignment
    == is the equality operator
    y is a variable named y
    'y' is a char

    now you should be able to fix it
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Re: Function problem

    Quote Originally Posted by Russco View Post
    = is assignment
    == is the equality operator
    y is a variable named y
    'y' is a char

    now you should be able to fix it
    Thank you sir. I knew it was a simple mistake.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Function problem

    One more:
    || is a logical OR
    | is a bit-wise OR
    (in this case produce the same result)
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Thumbs up Re: Function problem

    Quote Originally Posted by VladimirF View Post
    One more:
    || is a logical OR
    | is a bit-wise OR
    (in this case produce the same result)
    Yea, I had googled "or" statements. I was feeling more that "||" was the correct use of the "or" statement but I was having a hard time finding "||" as a valid use for "or" or even if it could be used. It also makes sense that it worked that way regardless of which one I used.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Function problem

    Also, this line:
    Code:
    std::cin>>a,b;
    Does not do what you think it does...

    Viggy

  11. #11
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Re: Function problem

    Quote Originally Posted by MrViggy View Post
    Also, this line:
    Code:
    std::cin>>a,b;
    Does not do what you think it does...

    Viggy
    Yes, I completely rewrote that function. It would have been nice to get it to take both numbers on the same line but I separated it and tossed in a switch for handling the yes or no pieces and error handling.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  12. #12
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Function problem

    Quote Originally Posted by fuzzylr View Post
    It would have been nice to get it to take both numbers on the same line ...
    Code:
    std::cin >> a >> b;
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  13. #13
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Question Re: Function problem

    I would like some advice. I would like to reduce the use of goto in this function even though it's fairly simple. What if I wanted to reuse bits and pieces of code? Can I do that or once I get into If / else or While loops do I need to move the code pieces inside the loop till it breaks out or what not. It seems kind of pointless to duplicate the code. I guess that is why the goto is so appealing in this type of instance.

    Code:
    int add()
    {
            char answer;
            int a,b;
            int total;
    
            start:
    
            std::cout<<"Enter first number: ";
            std::cin>>a;
            std::cout<<"Enter Second number: ";
            std::cin>>b;
    
            total = a+b;
            std::cout<<"\nSum: "<< total <<"\n\n";
    
            question:
            std::cout<<"Do you wish to continue: Y/N ";
            std::cin>>answer;
    
            switch (answer)
            {
                    case 'y': goto start;
                    case 'Y': goto start;
                    case 'n': goto end;
                    case 'N': goto end;
                    default:
                    std::cout<<"Invalid characters.  Use Y/N or y/n.\n\n";
                    goto question;
            }
    
            end:
            std::cout<<"Returning to menu";
            sleep(3);
            return 0;
    }
    would it be bad if I did cases like this?

    Code:
     case 'y' || 'Y': code sample here
    My Goal is to consolidate the code down as small as possible while still being functional.
    Last edited by fuzzylr; May 5th, 2011 at 01:47 PM.
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  14. #14
    Join Date
    Feb 2008
    Location
    Virginia
    Posts
    109

    Question Re: Function problem

    Quote Originally Posted by VladimirF View Post
    Code:
    std::cin >> a >> b;
    When you do that though. What happens if the user does [23] & [85] but types it out like this "2385"???
    Sean
    Programing 1
    Programing 2 (Spring this year)
    Eager Game Developer.

  15. #15
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Function problem

    Quote Originally Posted by fuzzylr View Post
    When you do that though. What happens if the user does [23] & [85] but types it out like this "2385"???
    Did you try?
    it will go into the first variable, and will sit there waiting for the second entry.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured