GetComputerName <-- Issue with string?
-
Hi all, got a question thats bugging me. I can not figure out this very simple compile error I am getting. Here is the error: C:\Documents and Settings\mininet\My Documents\C++\9APR09\test.cpp In function `int main()': 28 C:\Documents and Settings\mininet\My Documents\C++\9APR09\test.cpp expected primary-expression before "GetLocalComputerName" 28 C:\Documents and Settings\mininet\My Documents\C++\9APR09\test.cpp expected `;' before "GetLocalComputerName" I know its related to the string. but what is wrong with it? If you tell me whats wrong, dont just tell me. Please include a description of why the string is messed up. Thank you in advance! Code is as follows: <code>// Project 01 by Rob #include <iostream> #include <string> #include <windows.h> using namespace std; int main() { system("CLS"); string password; string GetLocalComputerName; cout << "password: "; cin >> password; if (password == "123"){ system("CLS"); // Cleares the screen cout << "password accepted" << endl;} else{ cout << "password incorrect" << endl; goto exit;} exit: system("CLS"); system("TITLE Local Computer Name"); string GetLocalComputerName() { TCHAR chrComputerName[MAX_COMPUTERNAME_LENGTH + 1]; string strRetVal; DWORD dwBufferSize = MAX_COMPUTERNAME_LENGTH + 1; if(GetComputerName(chrComputerName,&dwBufferSize)) { strRetVal = chrComputerName; } else { strRetVal = ""; } return(strRetVal); } //std::cin.ignore( std::numeric_limitsstd::streamsize::max(), '\y' ); return 0; } V/R Rob
-
Hi all, got a question thats bugging me. I can not figure out this very simple compile error I am getting. Here is the error: C:\Documents and Settings\mininet\My Documents\C++\9APR09\test.cpp In function `int main()': 28 C:\Documents and Settings\mininet\My Documents\C++\9APR09\test.cpp expected primary-expression before "GetLocalComputerName" 28 C:\Documents and Settings\mininet\My Documents\C++\9APR09\test.cpp expected `;' before "GetLocalComputerName" I know its related to the string. but what is wrong with it? If you tell me whats wrong, dont just tell me. Please include a description of why the string is messed up. Thank you in advance! Code is as follows: <code>// Project 01 by Rob #include <iostream> #include <string> #include <windows.h> using namespace std; int main() { system("CLS"); string password; string GetLocalComputerName; cout << "password: "; cin >> password; if (password == "123"){ system("CLS"); // Cleares the screen cout << "password accepted" << endl;} else{ cout << "password incorrect" << endl; goto exit;} exit: system("CLS"); system("TITLE Local Computer Name"); string GetLocalComputerName() { TCHAR chrComputerName[MAX_COMPUTERNAME_LENGTH + 1]; string strRetVal; DWORD dwBufferSize = MAX_COMPUTERNAME_LENGTH + 1; if(GetComputerName(chrComputerName,&dwBufferSize)) { strRetVal = chrComputerName; } else { strRetVal = ""; } return(strRetVal); } //std::cin.ignore( std::numeric_limitsstd::streamsize::max(), '\y' ); return 0; } V/R Rob
rbwest86 wrote:
string GetLocalComputerName()
What is this?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
rbwest86 wrote:
string GetLocalComputerName()
What is this?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Hi all, got a question thats bugging me. I can not figure out this very simple compile error I am getting. Here is the error: C:\Documents and Settings\mininet\My Documents\C++\9APR09\test.cpp In function `int main()': 28 C:\Documents and Settings\mininet\My Documents\C++\9APR09\test.cpp expected primary-expression before "GetLocalComputerName" 28 C:\Documents and Settings\mininet\My Documents\C++\9APR09\test.cpp expected `;' before "GetLocalComputerName" I know its related to the string. but what is wrong with it? If you tell me whats wrong, dont just tell me. Please include a description of why the string is messed up. Thank you in advance! Code is as follows: <code>// Project 01 by Rob #include <iostream> #include <string> #include <windows.h> using namespace std; int main() { system("CLS"); string password; string GetLocalComputerName; cout << "password: "; cin >> password; if (password == "123"){ system("CLS"); // Cleares the screen cout << "password accepted" << endl;} else{ cout << "password incorrect" << endl; goto exit;} exit: system("CLS"); system("TITLE Local Computer Name"); string GetLocalComputerName() { TCHAR chrComputerName[MAX_COMPUTERNAME_LENGTH + 1]; string strRetVal; DWORD dwBufferSize = MAX_COMPUTERNAME_LENGTH + 1; if(GetComputerName(chrComputerName,&dwBufferSize)) { strRetVal = chrComputerName; } else { strRetVal = ""; } return(strRetVal); } //std::cin.ignore( std::numeric_limitsstd::streamsize::max(), '\y' ); return 0; } V/R Rob
Hello Rob, You've defined
GetLocalComputerName()
insidemain()
. Nested function definition is illegal. So moveGetLocalComputerName()
outside to resolve the error. Regards, Jijo._____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
Hello Rob, You've defined
GetLocalComputerName()
insidemain()
. Nested function definition is illegal. So moveGetLocalComputerName()
outside to resolve the error. Regards, Jijo._____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
First off thank you very much. I read into the embeded function and your right, it will not work. I do however feel there is something missing. I relocated the entire section related to retrieving the local computer name, to outside int main(). I think there needs to be #include and have string GetLocalComputerName; cin >> or output somehow. Because the program compiles, yet it dosent seem as if it is doing anything. After I type in the password, it should jump to the next sequential portion of the program, and retrieve the username. It does nothing but stare back at me. I feel as if I am missing something, can someone point me in the right direction? I do a lot of reading but the reading needs to be put into perspective. Thank you very much in advance. V/R Rob
-
First off thank you very much. I read into the embeded function and your right, it will not work. I do however feel there is something missing. I relocated the entire section related to retrieving the local computer name, to outside int main(). I think there needs to be #include and have string GetLocalComputerName; cin >> or output somehow. Because the program compiles, yet it dosent seem as if it is doing anything. After I type in the password, it should jump to the next sequential portion of the program, and retrieve the username. It does nothing but stare back at me. I feel as if I am missing something, can someone point me in the right direction? I do a lot of reading but the reading needs to be put into perspective. Thank you very much in advance. V/R Rob
Hi Rob, You should call GetLocalComputerName() function and print it. For instance, see the corrected code snippet.
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;// Function moved outside.
string GetLocalComputerName()
{
TCHAR chrComputerName[MAX_COMPUTERNAME_LENGTH + 1];
string strRetVal;
DWORD dwBufferSize = MAX_COMPUTERNAME_LENGTH + 1;if(GetComputerName(chrComputerName,&dwBufferSize)) {
strRetVal = chrComputerName;
} else {
strRetVal = "";
}return(strRetVal);
}int main()
{
system("CLS");
string password;
cout << "password: ";
cin >> password;if (password == "123"){
system("CLS"); // Cleares the screen
cout << "password accepted" << endl;}else{
cout << "password incorrect" << endl;
goto exit;}
exit:system("CLS"); system("TITLE Local Computer Name"); // Print local computer name. cout << GetLocalComputerName(); return 0;
}
Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
it is a string, i could have called it anything. But I kept it familiar with the function. All I am trying to do is retrieve the local computer name.
rbwest86 wrote:
it is a string...
No, it's the beginning of a nested function, which is not allowed.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons