鱼C论坛

 找回密码
 立即注册
查看: 2860|回复: 5

[已解决]编写的C++程序有多余的输出

[复制链接]
发表于 2017-10-28 18:49:59 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 dt3tc 于 2017-10-28 20:22 编辑

如图 screenshot_20171028104534.jpg
#include <iostream>

struct car{
        char maker[15];
        int made_year;
};
int main(){
    using namespace std;
        cout<<" How many cars do you wish to catalog?";
        int quantity=0;
        cin>>quantity;
        cin.get();
        car a[quantity]={0};
        for(int b=0;b<quantity;b++){
                cout<<"Car #"<<b+1<<":\n";
                cout<<" Please enter the make:";
                cin.get(a[b].maker,14);
                cin.get();
                cout<<"Please enter the year made:";
                cin>>a[b].made_year;
                cin.get();
        };
        cout<<"Here is your collection:\n";
        for(int b=0;b<=quantity;b++)cout<<a[b].made_year<<" "<<a[b].maker<<endl;
        cout<<"goodbye"<<endl;

return 0;
}
谢谢
最佳答案
2017-10-28 18:50:00
dt3tc 发表于 2017-10-30 17:07
现在编译通过,但运行到最后仍然有多余的输出

最后的for循环条件<=改成<
这里越界了

最佳答案

查看完整内容

最后的for循环条件
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-10-28 18:50:00 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
dt3tc 发表于 2017-10-30 17:07
现在编译通过,但运行到最后仍然有多余的输出

最后的for循环条件<=改成<
这里越界了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-10-28 21:35:33 | 显示全部楼层
动态数组请用new配合delete进行处理
car a[quantity]={0};
改为
car * a[] = new car[quantity];
记得最后进行释放
delete [] a;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-10-30 16:31:18 | 显示全部楼层
BngThea 发表于 2017-10-28 21:35
动态数组请用new配合delete进行处理
car a[quantity]={0};
改为

改过了编译报错
||=== Build: Debug in 1 (compiler: GNU GCC Compiler) ===|
D:\mxf\src\tmp\1\main.cpp||In function 'int main()':|
D:\mxf\src\tmp\1\main.cpp|13|error: initializer fails to determine size of 'a'|
D:\mxf\src\tmp\1\main.cpp|13|error: array must be initialized with a brace-enclosed initializer|
D:\mxf\src\tmp\1\main.cpp|26|warning: deleting array 'a'|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
#include <iostream>

struct car{
        char maker[15];
        int made_year;
};
int main(){
    using namespace std;
        cout<<" How many cars do you wish to catalog?";
        int quantity=0;
        cin>>quantity;
        cin.get();
        car * a[] = new car[quantity];
        for(int b=0;b<quantity;b++){
                cout<<"Car #"<<b+1<<":\n";
                cout<<" Please enter the make:";
                cin.get(a[b]->maker,14);
                cin.get();
                cout<<"Please enter the year made:";
                cin>>a[b]->made_year;
                cin.get();
        };
        cout<<"Here is your collection:\n";
        for(int b=0;b<=quantity;b++)cout<<a[b]->made_year<<" "<<a[b]->maker<<endl;
        cout<<"goodbye2"<<endl;
        delete [] a;

return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-10-30 16:40:37 | 显示全部楼层
car * a[] = new car[quantity];
这句打错了,应该是
car * a = new car[quantity];
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-10-30 17:07:09 | 显示全部楼层
BngThea 发表于 2017-10-30 16:40
car * a[] = new car[quantity];
这句打错了,应该是
car * a = new car[quantity];

现在编译通过,但运行到最后仍然有多余的输出 screenshot_201710309447.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-30 23:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表