Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetComputerName <-- Issue with string?

GetComputerName <-- Issue with string?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rbwest86
    wrote on last edited by
    #1

    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

    D J 2 Replies Last reply
    0
    • R rbwest86

      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

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • D David Crow

        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

        R Offline
        R Offline
        rbwest86
        wrote on last edited by
        #3

        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.

        D 1 Reply Last reply
        0
        • R rbwest86

          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

          J Offline
          J Offline
          Jijo Raj
          wrote on last edited by
          #4

          Hello Rob, You've defined GetLocalComputerName() inside main(). Nested function definition is illegal. So move GetLocalComputerName() outside to resolve the error. Regards, Jijo.

          _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

          R 1 Reply Last reply
          0
          • J Jijo Raj

            Hello Rob, You've defined GetLocalComputerName() inside main(). Nested function definition is illegal. So move GetLocalComputerName() outside to resolve the error. Regards, Jijo.

            _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

            R Offline
            R Offline
            rbwest86
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • R rbwest86

              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

              J Offline
              J Offline
              Jijo Raj
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • R rbwest86

                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.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups