Error With Transfering Variables! Please help!!

hey everybody, im trying to make a pretty big program and ive gotten some major errors in 2 lines: heres the code:

main.cpp:

#include <iostream>
using namespace std;
#include <string>
#include <unistd.h>
void login(){
extern string Username;
extern string Password;

cout << "Enter Your Username: ";
cin >> Username;
cout << " "<<endl;
extern string Username = extern string user; //takes the username and sets it to user
extern string user = string USR; //takes usr and sets it to USR
if (Username = USR){ //if username is usr (if the username is what you registered to be)
cout << "Enter Your Password";
cin >> Password;
cout << " "<<endl;
}


}

void reg(){
extern string user;
extern string pass;

cout <<"Create A Username: ";
cin >> user;
cout << " "<<endl;
cout << "Create A Password: ";
cin >> pass;
cout << " "<<endl;
cout << "Your Username Is: "<<user<<endl;
cout << "Your Password Is: "<<pass<<endl;
extern string user = string Username;
extern string pass = string Password;
mainframe();
}

void mainframe(){
cout << "*******************************************************************";
cout << "* *";
cout << "* *";
cout << "* -MAINSPACE- *";
cout << "* *";
cout << "* 1.Login *";
cout << "* 2.Register *";
cout << "* 3.Exit *";
cout << "* *";
cout << "* *";
cout << "* *";
cout << "* *";
cout << "*******************************************************************";
int Answer;
cout << "Type The Operation Number: ";
cin >> Answer;

if (Answer == 1){
login();
}
else if (Answer == 2){
reg();
}
else if (Answer == 3){
cout <<"Thank You For Using G-OS";
sleep(1);
cout <<"Shutting Down";
sleep(1);
cout <<"Goodbye";
}
}
void CLS(){//clear screen method
for (int iLoop = 0; iLoop < 40; iLoop++){
cout << " ";
}
mainframe();
}

int main()//main method
{
cout << "Welcome User To G-OS"<<endl;
sleep(1);
cout << "Loading MainFrame"<<endl;
sleep(1);
cout << "Loading...."<<endl;
sleep(1);

cout << "***********************************************************"<<endl;
cout << "****** MAINFRAME LOADED ******"<<endl;
cout << "***********************************************************"<<endl;

sleep(3);
CLS();

return 0;
}



global.cpp:

#include "global.h"
#include <string>
global::global()
{
string user;
string pass;
}

global::~global()
{
//dtor
}


errors:

-------------- Build: Debug in MainFrame ---------------

Compiling: main.cpp
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp: In function 'void login()':
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:12: error: 'Username' has both 'extern' and initializer
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:12: error: declaration of 'std::string Username'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:6: error: conflicts with previous declaration 'std::string Username'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:12: error: expected primary-expression before 'extern'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:12: error: expected ',' or ';' before 'extern'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:13: error: 'user' has both 'extern' and initializer
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:13: error: expected primary-expression before 'USR'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:13: error: expected ',' or ';' before 'USR'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:14: error: 'USR' was not declared in this scope
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:13: warning: unused variable 'user'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp: In function 'void reg()':
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:35: error: 'user' has both 'extern' and initializer
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:35: error: declaration of 'std::string user'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:24: error: conflicts with previous declaration 'std::string user'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:35: error: expected primary-expression before 'Username'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:35: error: expected ',' or ';' before 'Username'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:36: error: 'pass' has both 'extern' and initializer
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:36: error: declaration of 'std::string pass'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:25: error: conflicts with previous declaration 'std::string pass'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:36: error: expected primary-expression before 'Password'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:36: error: expected ',' or ';' before 'Password'
/Users/rabbiandrewbloom/Desktop/C++/MainFrame/main.cpp:37: error: 'mainframe' was not declared in this scope
Process terminated with status 1 (0 minutes, 0 seconds)
20 errors, 1 warnings



what im trying to do is set the username and password in login() to mimic the ones set in reg() (i want to make it transfer over so you can set one in reg() and use it in login() but im getting errors)

PLEASE HELP ITS VERY IMPORTANT!!
Last edited on
You should read some book on C++ for beginners.

You may not define a function within a definition of other function

1
2
3
4
void login(){
 extern string Username;
 extern string Password;
 void mainframe(){
Last edited on
^but how do i make it so that it takes the username and password from the register method to the login method?
It is another question. I pointed out the reason of the errors. So correct your program at first.
^ i fixed some of the code, please read it again (its updated)
This statement is incorrect

extern string Username = extern string user; //takes the username and sets it to user

because extern string user is a declaration while the assignment statement requires in the right side an expression.

Also I do not understand such comments as

extern string user = string USR; //takes usr and sets it to USR

What does it mean?!
Last edited on
I messed around with this for a while, and this is REALLY messed up code.

You don't seem to understand the concept of passing variables between functions. You do it like this.

Say you have one function, called FunctionA, which takes two integers and returns them multiplied:

1
2
3
4
int FunctionA(int a, int b)
{
  return a*b;
}


To call this function, you do it like this:

1
2
3
4
5
int main()
{
  int result = FunctionA(3, 4); // result will equal 12
  return 0;
}


You could also pass two initialised variables, like this:

1
2
3
4
5
6
7
int main()
{
  int anInteger = 4;
  int anotherInteger = 5;
  int result = FunctionA(anInteger, anotherInteger);
  return 0;
}


Another problem you have is that whenever you set a variable's value, you always do it wrong. You seem to do it like this:

 
string lolwat = string ohoho;


If you're trying to set lolwat to be equal to ohoho, then ohoho would have to be a previously-initialised variable, and you'd do that like this:

1
2
string ohoho = "STRING AND STUFF";
string lolwat = ohoho;


You need to read the basic documentation on C++ that this site offers:
http://cplusplus.com/doc/
^so, for the third time, how would i use that concept to bring the registered user and password to the login?????
Well, I would read the documentation, and read the stuff about object-oriented programming, and classes and such. I would then create a class which contained the username and password (perhaps encrypted if you are able to), and then your functions could pass variables of that class type around as arguments, and use those.

I'm not gonna write all that out for you, because this is a help forum, not a code request forum. Read how to use C++ in the documentation. Learning new features of the language and practising them is the only way forwards.

EDIT: You could even work out a way to get the user data stored in binary format in files. You just need to get used to the C++ language first.
Last edited on
Topic archived. No new replies allowed.