How do I have undeclared functions?

Here is my random code. Im trying to figure out what Im doing wrong here. Anyone help?

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <conio.h>

using namespace std;
inline void keep_window_open() { char ch; cin>>ch; }

int main()
{
	cout << "Welcome to the Classified Network, DOD842349729961971\n";
    cout << "Username: \n";
	string admin = "gardinerca";
	string root_password = "password1";
    string full_name = "Casey Gardiner";
	string name;
    cin >> name; 
    if (name == admin)
	   {
       printf("Password: ");
       char password[10];
       int i;
       for (i = 0; i < sizeof password - 1; i++)
           {
           int c = getch();
       if (c == '\n' || c == EOF)
       break;
           }
       password[i] = c;
       printf("*");
       }
       password[i] = '\0';
                 if (strcmp(password, root_password)) == 0)
                   {
                        system("CLS");
                        cout << "Welcome " << full_name << " to the Classified Network\n";
                        cout << "Would you like to play a game? (Y or N)\n";
                        string play_game;
                        cin >> play_game;
                        if (play_game == "Y");
                           {
                            cout << "How many balls can you stick in your mouth?\n"
                            int balls
                            cin >> balls
                            string one
                            string two
                            one = "One Ball"
                            two = "Two Ball's"
                            while (balls == 1);
                            {
                                      balls = one
                                      cout << "You can honestly stick " << one << " in your mouth?";          
                            }          
                        else 
                             {
                             cout << "You have selected the No Option. Thats fine...we don't want to play with you either\n";                                       
                             }
                   }
           else
               {
                        cout << "Invaild Password. Please contact system administrator.\n";
                        cin.clear(); 
                        system ("PAUSE");
               }

    else 
         {
         cout << "No Username found. Please contact system administrator.\n";
         cin.clear(); 
         system ("PAUSE");
         }               	
return 0;
}
See lines 32, 36, and 54. You have errors in those lines, which should be obvious once you see them. You also forgot to #include <cstring> to use strcmp() but some compilers auto-include it for you.

Additionally, check lines 43 and 52. Are you sure you want those ; there?

Note that line 72, although the intuitive solution, doesn't do what you think it does. :(

EDIT: Check line 26. You're not using sizeof correctly there. Firstly, you're missing ( ), and it returns a size in bytes. In place of the whole hoopla with sizeof, you could've just put 10. The -1 hinders you, as the < is a less than, not less than or equal to operator.

Last, I'd like to clear a few things up about your programming etiquette, if you don't mind. :/
Firstly, never use system(), as it's not only slow and platform dependent, but a massive security hole.
Second, why are you mixing C and C++ I/O, by using printf() and std::cout in the same program?
Third, why are you mixing C and C++ strings?
Foruth, conio.h has been deprecated. Avoid it like the plague. ;)
Finally, you have a ton of headers you don't need. <vector>, <algorithm>, and <cmath> are unneeded here.

Keep working! You'll manage just fine! ;)

-Albatross
Last edited on
I mixed strings because I had no other way of displaying astrixs for the password, which was the only reason I made this to begin with. Is line 36 suppose to be if (strcmp(password, root_password) == 0) ? Im still getting a missing function error on that line.
Im using conio.h because its needed for getch.
There's no standard way of creating a backspace, although I'd suggest that if you want to do more fancy console work like this, that you use the PDCurses library. It's portable and far better than using conio.h, as the functionality doesn't change between compilers (!). Just note that it doesn't mix too well with the standard library functions and objects.

Oh... and about the missing function... look carefully at the arguments. I just realized something that I knew I was forgetting about. strcmp() doesn't take strings as arguments; you need to use std::string::c_str() to get a C-string from a C++-string.

-Albatross
Last edited on
Topic archived. No new replies allowed.