wrong code

Pages: 12
closed account (9SvpX9L8)
i have this code
what is wrong with him

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
/* search_engine.cpp */

#include <iostream>
#include <string>
using namespace std;

void first_screen (int log_in , int creat , int guest);
char name[20]; password[6];

int main ()
{
	cout << "Hello Guest \n";
	cout << "What would you like to do: \n";
	first_screen ;
}
   void first_screen (int log_in , int creat , int guest); 
	{
	  switch (first_screen)
	{
	      case 'L':
	      cout <<" [L]og in for an existing account. \n";  
	      string str;
       	      cout << "User Name"; getline (cin,str);
	      cout <<" Password"; cin >> password;
                     cout <<"Welcome Back ";  << User Name ;
	      cout <<" Press [H]istory for see your search history"; break;
	      case 'S':
	      cout <<" [S]ign in. \n";
	      cout <<"\t Welcome for Search Engine ! \n";
	      cout <<"User Name: \n"; getline (cin,str);
	      cout <<"Password (6 numbers max);  \n"; cin >> password ; 
	      cout <<"Re-type your password: "; cin >> password ; 
	      cout <<"Question for restore your password: \n"; cin >> question ; 
	      cout <<"Answer: \n"; cin >> answer ; break;
                    case 'G': 
	     cout <<"Stay [G]uest. \n"; break ;
		}
	} 


Last edited on
Allow us to remind you that first_screen is a function, and must be called with arguments and a call operator (). You would probably also do well to remember the return in main, and convert that first semicolon on line 8 to a comma.
Then reindent the function and come back to us. (Seriously though, I don't think I can see anything wrong with the function, I'd just stop stacking multiple statments up onto one line.)
And "him"?
EDIT: OK, some quick review revealed another error on line 25. I'm sure you can figure it out. (It's when statements get clumped up like this that that happens.)
Last edited on
closed account (9SvpX9L8)
ok, i put a ", " in line 8 .
the code still not running.
i have error in line 14 and 17
Line 14's error I addressed in my last post.
And you cannot switch on a function. A function is not a variable or expression; you cannot define a switch to go around it.
What are you trying to do anyway? That will help you determine what you should be doing in the function.
closed account (9SvpX9L8)
well i'll pm you ok ?
First things first as stated above you should try to place everything in separate lines so your code is easier to read. However you have 3 mistakes.

If you look at your code this line is incorrect:
char name[20]; password[6];

Reason:
You can not have the ; after a type declaration and have the next variable name be associated with that type variable. The line needs to be

Correction:
char name[20], password[6]; will work however much nicer to do this

char name[20];
char password[6];

Next minor problem is the fact you didn't close out your function after using the switch statement. You closed the switch statement but never closed the function therefore the compiler has no clue as to when the function first screen ends.

Your next error is both logic error and syntax error in your switch statement. Your switch statement reads switch(first_screen) which is ok if first_screen returned some type of simple variable type however first screen is a void function thus returns nothing. Depending on what your want your switch statement (which appears to want a char value).

Look at those and then ask again if you need more info on what your doing wrong.

@jshafferman, the function does have a complete set of curly brackets. The indenting is just really bad. And the switch would still be wrong if firstscreen had a return - firstly because the recursion would be infinite and secondly because you still need to provide a call operator and the required arguments.
Last edited on
closed account (9SvpX9L8)
it's a bit difficult for me to understand it in English so i will ask maybe stupid question, sorry.

error in line 14::
function call missing argument list.
error 17:
'{' missing function header (old-style normal list?)

well , i read the code again i have } for closing my switch and then another } for closing the function
closed account (9SvpX9L8)
tummy , if i understand u good , u say i fall to recursion ? how can i fix it?
where is my recursion ?
where should i put my return ?
what wrong with my switch statment?
@tummychow, whoops I forgot that the infinite recursion would occur in this situation and also the obvious need for the correct call to the function in general but I thought that was already stated earlier but anyhow, yes the correct call with correct arguments is needed for the function to be ran regardless of how you intend to program it.

@snoopy as tummychow stated you must call your function with the correct arguments in your case first_screen(int , int, int) as you have it right now.
You need a base case for recursion otherwise you will get infinite recursion. You would need some case statement to end the program, such as:

default:
return; // for a void function

The way you have it coded it will run forever because the function will just keep calling itself until the activation stack is filled up then your program will just fail at that point.
No, I'm saying there would be infinite recursion if you called your function in the switch. You probably shouldn't do that anyway. (unless you had good reason but I don't even know what you are trying to do.)
Line 17's error is a result of the semicolon at the end of line 16. When you define a function you do not end its argument list with a semicolon.
I'll be really clear for line 14. I don't want to sound like I'm babying anybody though something but... you asked. When you refer to a function, you must include the call operator thereafter:
1
2
3
4
5
void foo();
int main()
{
    foo(); // The bold part is the call operator
}

Even if you have no arguments you must put the call operator there, otherwise you are not calling a function and will probably collide headfirst into an error.
If you have arguments you must include those as well:
1
2
3
4
5
void foo(int, char);
int main()
{
    foo(3, 'c'); // The bold part is the call operator
}

So when you refer to firstscreen, since it is a function you must *call* it.
Last edited on
closed account (9SvpX9L8)
well i did that ,
int log_in, int creat, int guest

u say its wrong?
what is that correct call ?
snoopy the tiger:
'{' missing function header (old-style normal list?)
Misplaced semicolon at the end of line 16.

Use either spaces or tabs for indentation, but not both. Otherwise, the structure gets screwed up when the size of tab changes (you're using tabs of size 4 and the website is using 8).
closed account (9SvpX9L8)
i delete the semicolon from line 16 and i got a long list of error ...
i need to "return" to where?
i wrote return first_screen;
You DO NOT return; the function is void.
You might want to brush up on your functions; give this a look:
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
closed account (9SvpX9L8)
can someone please write me the correct code??
thanks
i did a bit of modification.Please tell me what do you think?


/* search_engine.cpp */

#include <iostream>
#include <string>
using namespace std;

void first_screen (int choice);
char name[20], password[6];
int choice;

int main ()
{
    do
    {
    	cout << "Hello Guest \n";
    	cout << "1.Log in\n";
    	cout << "2.Create\n";
    	cout << "3.Guest\n";
    	cout << "What would you like to do: ";
    	cin >> choice;
    	first_screen (choice);
    }while (choice < 3);
    return 1;
}



void first_screen (int choice) 
{
    string str;
    string question;
    string answer;
    
    switch (choice)
    {
        case 1:
            cout <<" [L]og in for an existing account. \n";  
            cout << "User Name:";
            while (cin.get() != '\n') continue;
            getline (cin,str);
            cout <<"Password:";
            cin >> password;
            cout <<"Welcome Back '" << str << "'" << endl;
            cout <<"Press [H]istory for see your search history\n";
            break;
	case 2:
	    cout <<" [S]ign in. \n";
	    cout <<"\t Welcome for Search Engine ! \n";
	    cout <<"User Name: \n";
	    while (cin.get() != '\n') continue;
            getline (cin,str);
            cout <<"Password (6 numbers max);  \n";
            cin >> password ;
            cout <<"Re-type your password: ";
            cin >> password ;
            cout <<"Question for restore your password: \n";
            cin >> question ;
            cout <<"Answer: \n";
            cin >> answer ;
            break;
        case 3: 
            cout <<"Stay [G]uest. \n";
            break;
        default:
            cout << "Not such option";
            break;
	}
} 
Last edited on
Code tags not output tags.
Not bad. If that's what the OP actually wanted then it should work fine.
(Although there's no reason for any of those variables to be global actually. The password and name are not referred to in main so they can be declared in the function, and choice can be declared in main and is passed, so no need for that to be global either. I personally avoid using globals if I can.)
(And lastly, you can and should probably replace those char[] with strings. More effective and reliable.)
Last edited on
Why would you write it for him??? Let him do it himself, but tell him how. That way he learns. What's the point of writing it for him? He doesn't learn anything that way!
Pages: 12