Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;年龄赢了';t显示(结构)_C++_Pointers_Struct_Runtime Error - Fatal编程技术网

C++ C++;年龄赢了';t显示(结构)

C++ C++;年龄赢了';t显示(结构),c++,pointers,struct,runtime-error,C++,Pointers,Struct,Runtime Error,我已经用struct和指针写了一段代码,问题是当你输入age时,结果是0。如果你看一下我的代码,会很有帮助的 Person's name: Billy Person's age: 25 Write your hobby[0]:Kungfu Write your hobby[1]:soccer write your hobby[2]:basketball Write your crush name: Jake Name:Billy Age:

我已经用struct和指针写了一段代码,问题是当你输入age时,结果是0。如果你看一下我的代码,会很有帮助的

Person's name: Billy
    Person's age: 25
    Write your hobby[0]:Kungfu 
    Write your hobby[1]:soccer
    write your hobby[2]:basketball
    Write your crush name: Jake
    Name:Billy
    Age:0
    Hobbies[0]:Kungfu
    Hobbies[1]:soccer
    Hobbies[2]:basketball
    Crush: Jake
出于某种原因,年龄被指定为25岁,但结果显示为0,为什么会这样

#include <iostream>
#include <string>
#include <Windows.h>
#include <sstream>
using namespace std;

//declare structure to store info about Billy
struct Son{
    string name;
    string crush;
    int age;
    string hobbies[3];
}Person;

int main(){
    string sAge;
    int i;
    Son* info = new Son;
    info = &Person;
    //user interface
    //Person's name
    cout << "Person's name: ";
    getline(cin, info ->name); //inputs person's name
    //Person's age
    cout << "Person's age: ";
     //inputs person's age
    getline(cin,sAge);
    (stringstream)sAge << info ->age; 
    //for loop to get hobbies
    for(i = 0; i < 3; i++){
        cout << "Write your hobby[" << i <<"]: ";
        getline(cin,info ->hobbies[i]); //inputs the person hobby three times
    }
    //Person's crush
    cout << "Write your crush name: ";
    getline(cin, info ->crush); //inputs the person's crush *opitional*

    //output statement
    cout << "Name: " << info ->name << endl; //display name
    cout << "Age: " << info ->age << endl; //display age
    for(int j = 0; j < 3; j++){ //display hobbies
    cout << "Hobbies[" << j << "]: " << info ->hobbies[j] << endl;
    }
    cout << "Crush: " << info ->crush << endl; //display crush                                             
    delete info;
    system("pause");
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//声明结构以存储有关Billy的信息
结构子{
字符串名;
压线;
智力年龄;
弦乐爱好[3];
}人;
int main(){
串鼠尾草;
int i;
儿子*info=新儿子;
信息=&Person;
//用户界面
//人名
cout name);//输入人名
//人的年龄
cout
(stringstream)sAge age;

这不是正确的方法,您可以使用以下任一方法:

stringstream ss;
ss << sAge;
ss >> info->age;
stringstreamss;
ss>信息->年龄;
info->age=atoi(sAge.c_str());

或者,您可以使用
std::atoi(sAge)
,它立即接受
std::string
,但是,因为您已经使用了
名称空间std;
它应该自动使用它。感谢@alexolute的说明

<>你基本上是将代码>字符串 >代码> String Strue/Cuff>,这是一个巨大的失败, String Strue不是 String 的子类。也不应该在C++中使用C风格的Casic,学习使用<代码> StasyType > <代码>,<代码> RealTytCase和<代码> DyrimCysCase<代码>以生成更好的警告。(感谢@JohnZwinck的陈述)


旁注:您包括了
Windows.h
,并且没有使用它的任何功能。

使用所有警告和调试信息编译(例如
g++-Wall-g
)。改进代码直到你没有收到警告。学习如何使用调试器,如
gdb
Son*info=new-Son;info=&Person;
为什么
info
是动态定位的?为什么要将其设置为Person的地址?这是内存泄漏!兄弟,1)我没有一个C++初学者,所以不知道@ BaselestalyKevisti没有MAC(只有运行Linux / Debian/SID的PC)“吉姆,你需要什么MAC?只需安装一些调试器,比如<代码> GDB < /C>或代码> CDB<代码>,如果VisualStudio.GDB在每个平台上都是工作的,一般来说,你不应该在C++中使用C风格的Casc.,总是有一个更安全的可用:COSTYSTCAST,STATICECAST,DIAMICICOCAST,或RealTytPayCase. @吉姆,你应该真正阅读C++标准的书,以了解所有的C++ STD库必须提供。或检查一些文档。签出而不是ATOI(),您可以使用STD::Studio(),它接受STD::String参数(不需要通过CXString()方法获得C字符串。这可以是更多的C++方式。