开发者

console app c++

开发者 https://www.devze.com 2022-12-11 16:57 出处:网络
i\'m new to console apps and would appreciate some pointers... i have created a new console app and (not finished but it should be working), i selected win32 console app and then selected \'empty pro

i'm new to console apps and would appreciate some pointers...

i have created a new console app and (not finished but it should be working), i selected win32 console app and then selected 'empty project'

here's my code:

#include <iostream>

void main() {

struct dude { 
    string name; 
    int age; 
} about; 

about.name = "jason"; 
about.age = 4000; 
cout << about.name << " " << about.age << endl;
}

The following errors i get are:

------ Build started: Project: Test, Configuration: Debug Win32 ------
Compiling...
codey.cpp
.\codey.cpp(6) : error C2146: syntax error : missing ';' before identifier 'name'
.\codey.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
.\codey.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
.\codey.cpp(10) : error C2039: 'name' : is not a member of 'main::dude'
        .\codey.cpp(5) : see declaration of 'main::dude'
.\codey.cpp(12) : error C2065: 'cout' : undeclared identifier
.\codey.cpp(12) : error C2039: 'name' : is not a member of 'main::dude'
        .\codey.cpp(5) : see declaration of 'main::dude'
.\codey.cpp(12) : error C2065: 'endl' : undeclared identifier
Build log was saved at "file://c:\Users\Jason\Documents\Visual Studio 2008\Projects\Test\Test\Debug\BuildLog.htm"
Test - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

can somebody please tell me how i can get this to debug? what have i done wrong here?


Working...

#include <iostream> 
#include <string> 

struct Dude { 
    std::string name; 
    int age; };

    int main(int i) 
    { 
        while(i<4000)
        {
            i++;
        using namespace std; 
        Dude jason = { "Jason", i }; 
        cout << jason.name << " is " << jason.age << " years old.\n"; 
        }

        return 0; 
    }

thank you all for your开发者_Go百科 help :D


#include <iostream>
#include <string>

struct Dude {
  std::string name;
  int age;
};

int main() {
  using namespace std;
  Dude jason = { "Jason", 4000 };
  cout << jason.name << " is " << jason.age << " years old.\n";
  return 0;
}

In no particular order:

  • define your types outside of functions
  • name types consistently ("Dude" here)
  • provide values when initializing objects
    • I've used aggregate initialization here, you will use slightly different syntax when you write a constructor (ctor)
  • the C++ stdlib is almost exclusively in the std namespace
    • you can place using namespace std; at function scope, if you like, instead of typing std:: within that function body
  • the C++ stdlib string type is from the <string> header
  • main returns int

Fixing these problems isn't really debugging, you just need to learn the correct syntax. Make sure you have a good book (e.g. Accelerated C++ by Koenig and Moo) and a good teacher doesn't hurt.


The correct code should look like:

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

struct dude {
  string name;
  int age;
};

int main() { 

  struct dude about;
  about.name = "jason";
  about.age = 4000;

  cout << about.name << " " << about.age << endl;
  return 0;
}

EDIT: Added the necessary includes so that it compiles. Also, as a best practise, moved type definition outside of function.


In addition to Joy's answer, you need to use the std namespace:

std::cout << about.name << " " << about.age << endl;

Also, what are you trying to achieve? Maybe it's better if dude were a class?


You mean how to get it to compile!

You forgot to put

using namespace std;

I think you should pick up a good C++ book, spend some time reading it and come to SO like site afterwards. Just a sincere advise, no offense.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号