|
-
May 3rd, 2011, 03:41 PM
#1
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 âint add()â:
functions.cpp:9:11: error: âyâ was not declared in this scope
functions.cpp:20:34: error: âYâ 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.
-
May 3rd, 2011, 03:45 PM
#2
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);
-
May 3rd, 2011, 03:47 PM
#3
Re: Function problem
 Originally Posted by GCDEF
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.
-
May 3rd, 2011, 03:50 PM
#4
Re: Function problem
 Originally Posted by GCDEF
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.
-
May 3rd, 2011, 03:55 PM
#5
Re: Function problem
 Originally Posted by GCDEF
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.
-
May 3rd, 2011, 03:57 PM
#6
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.
-
May 3rd, 2011, 03:58 PM
#7
Re: Function problem
 Originally Posted by Russco
= 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.
-
May 3rd, 2011, 04:30 PM
#8
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...
-
May 5th, 2011, 08:34 AM
#9
Re: Function problem
 Originally Posted by VladimirF
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.
-
May 5th, 2011, 11:32 AM
#10
Re: Function problem
Also, this line:
Does not do what you think it does...
Viggy
-
May 5th, 2011, 01:36 PM
#11
Re: Function problem
 Originally Posted by MrViggy
Also, this line:
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.
-
May 5th, 2011, 01:43 PM
#12
Re: Function problem
 Originally Posted by fuzzylr
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...
-
May 5th, 2011, 01:44 PM
#13
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.
-
May 5th, 2011, 01:45 PM
#14
Re: Function problem
 Originally Posted by VladimirF
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.
-
May 5th, 2011, 01:50 PM
#15
Re: Function problem
 Originally Posted by fuzzylr
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|