help with creating a function

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
33
34
35
36
37
38
39
/*
5-40 String is on the List?

Design a function() to Confirm that a Certain String is on a List. 
*/
#include <iostream>
#include <string>
using namespace std;

string one (string a, string b, string c)
{
return  if ( a  == "two" )
 {cout << "You got it! It was two!\n";}
 else if ( b  == "two" )
 {cout << "You got it! It was two!\n";}
  else if ( c  == "two" )
 {cout << "You got it! It was two!\n";}
 else 
 {cout << "Try again!\n";};;
  
}

void main()
{
 string one, two, three;
 cout << "Give me a list of three numbers, spelled out, and I'll tell you if you have the number on my mind.\n";
  
  cout << "enter first number: \n";
 cin >> one;
  cout << "enter second number: \n";
  cin >> two;
   cout << "enter third number: \n";
 cin >> three;
 
cout >> string (one);

system ("pause");
}



heey, i think its not possible to do that for a function... can someone help me?
You might wanna correct the func with "return if(blah blah)",seems its where the error lies
First of all, int main(), not void main().

Second of all, the function's not written correctly...
1
2
3
4
5
6
7
void one (string a, string b, string c)
{
    if (a == "two" || b == "two" || c == "two") // No "return"
        cout << "You got it! It was two!\n";
    else
        cout << "Try again!\n";
}


Third of all, you have a function named "one", and also a string variable named "one".
You can't do that...(in other words, rename the function).

And fourth of all, you're not calling it correctly. (on line 35)
You should be calling it like
theNewNameOfMyFunctionHere(one, two, three); // No cout

Of course, that's not exactly how I would've written this program, but this will at least fix your error(s). (and give you the correct output)

EDIT: Wow, you beat me by just a few seconds...
Last edited on
yeaah . i think the function i made isnt possible.. right? how would i create a function that checks if a string is on the list then?

Last edited on
edit: ohh sorry about the fourth and third mistakes. i kinda gave up at the end when i saw it wasnt gonna work so i was being hasty.

I was wondering, why do we use void for the function one? what does void mean? and is there no return because we're not returning out something?
Try to fix the errors first,then try other alternatives...be innovative(thats why you're called a computer scientist),now if it doesnt work--call for help.People will help by then
magadavixt wrote:
I was wondering, why do we use void for the function one? what does void mean? and is there no return because we're not returning out something?
void means the function doesn't return anything.

I guess if you really wanted to...
1
2
3
4
5
6
7
string one (string a, string b, string c)
{
    if (a == "two" || b == "two" || c == "two")
        return "You got it! It was two!\n";
    else
        return "Try again!\n";
}


And then your function call would be
cout << stillNeedToRenameThisFunction(one, two, three);
Last edited on
OHH! thanks! i get it now. LOOOL.
Topic archived. No new replies allowed.