注册 登录
编程论坛 C++教室

程序运行总是出错,不知道为什么,请教各位帮忙

abraham1990 发布于 2011-01-10 09:53, 1332 次点击
//getinfo.cpp--input and output
#include<iostream>
int main()
{
    using namespace std;
    int carrots;
    cout<<"how many carrots do you have?"<<endl;
    cin>>carrots;       //c++input
    cout<< "here are two more.";
    carrots=carrots+2;
    //the next line concatenates output
    cout<<"now you have"<<carrots<<"carrots."<<endl;
    return 0;

}
这是我按书上编的一个程序,应该是不会错的,请各位大侠帮忙看看,我用的是microsoft visual studio 2008 .net的版本

首先有个问题,为什么每次生成的程序总是被VS显示为已经过时了,这是什么情况

运行之后显示有两个错误,不知道是什么错误。。。谁能帮忙
我是刚入门的那种,对C++的了解很浅,谢谢帮助哈!!!
15 回复
#2
li_danwang2011-01-10 10:21
VC6.0  运行正常   哈哈.
#3
qq10235692232011-01-10 21:38
#include<iostream>
using namespace std;  //你怎么把这句写在main()里面去了?
int main()
{
     int carrots;
    cout<<"how many carrots do you have?"<<endl;
    cin>>carrots;       //c++input
    cout<< "here are two more.";
    carrots=carrots+2;
    //the next line concatenates output
    cout<<"now you have"<<carrots<<"carrots."<<endl;
    return 0;
}
可能是过时了吧。 新出来的吗?好东西啊。我的2003的老是装不上

#4
xueshui202011-01-10 23:56
命名空间using namespace std;放错位置了
#5
找工作中2011-01-11 07:22
1. using namespace std;没有放错位置,这两个位置都可以的
2. 即使是using namespace std;放错位置导致的错误,应该也是编译错误,而不是运行错误
3. 这段程序虽然写得不好,但是应该不会有运行错误。如果有错误的话,lz至少应该说出是什么错误(错误总有信息/对话框之类的吧,否则你凭什么说出错了呢)。
#6
xdzsm2011-01-11 14:27
C-Free 下正常!显示  how many carrots do you have?
#7
abraham19902011-01-11 23:03
我试过很多方法。。。。
很多朋友帮忙看过了。。。。。。
最终是这样的程序。。。。



#include<iostream>
#include<string>
int main()
{
     using namespace std;
     char name[20];
     char address[20];
     cout<<"please input your name"<<endl;
     gets(name);
     cout<<"please input your address"<<endl;
     gets(address);
     cout<<"your name is "<<name<<endl;
     cout<<"your address is"<<address<<endl;
     return 0;
}
帮忙解释一下先。。。。看不懂。。。。char和gets()一般有什么用。。。。
c:\documents and settings\administrator\my documents\visual studio 2008\projects\24asfsaf\24asfsaf\erfa.cpp(11) : error C2679: 二进制“>>”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)
运行时候会看到这个。。。一直是这样。。。。总是会出现这个。。。。
请问这是什么情况???
#8
abraham19902011-01-11 23:07
还有朋友这样帮忙解决的:
//my first.cpp
#include<iostream>
#include<string>
int main()
{
    using namespace std;
    string name ;
    string address;
    cout<<"please input your name"<<endl;
    cin>>name;
    cout<<"please input your sddress"<<endl;
    cin>>address;
    cout<<"your name is "<<name<<endl;
    cout<<"your address is"<<address<<endl;
    return 0;
}
c:\documents and settings\administrator\my documents\visual studio 2008\projects\24asfsaf\24asfsaf\erfa.cpp(11) : error C2679: 二进制“>>”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)
谢谢各位热心帮忙看看
#9
abraham19902011-01-11 23:08

#include<string>
int main()
{
     using namespace std;
     string name1,name2;
     string address1,address2;
     cout<<"please input your name"<<endl;
     cin>>name1>>name2;
     cout<<"please input your address"<<endl;
     cin>>address1>>address2;
     cout<<"your name is "<<name1<<name2<<endl;
     cout<<"your address is"<<address1<<address2<<endl;
     return 0;
}
还有这个。。。。。好难啊
#10
zgxyz20082011-01-12 15:19
楼主,我都编译了一次,都能正常执行,是不是你建项目时没建对哦,要不就是RP的问题了~
#11
abraham19902011-01-12 21:13
我感觉是RP有些问题。。。。以前用vba的时候同样的代码,人家打进去就行,我运行就一定出错。。。。对了,有没有可能是因为VS版本的问题?我总觉得自己的vs2008编译的时候会出问题,。。。。
#12
abraham19902011-01-12 21:19
建项目的时候应该没问题吧。。。。我就是按书上的指示建的项目啊
#13
abraham19902011-01-12 21:28
可不可能是版本的问题?每次运行的时候都会说项目过时。。。。我用的是vs2008
#14
点线面2011-01-12 22:47
像VC这样的工具,如果搞得不好,重装电脑有这个可能
#15
DestinyLord2011-01-16 11:15
建议看看是不是建错项目,或者头文件名打错了.如果都对那建议LZ查查VS2008的语言是不是改了.
#16
找工作中2011-01-17 08:57
以下是引用abraham1990在2011-1-11 23:03:56的发言:

我试过很多方法。。。。
很多朋友帮忙看过了。。。。。。
最终是这样的程序。。。。



#include
#include
int main()
{
     using namespace std;
     char name[20];
     char address[20];
     cout<<"please input your name"<
     gets(name);
     cout<<"please input your address"<
     gets(address);
     cout<<"your name is "<<
     cout<<"your address is"<<
     return 0;
}
帮忙解释一下先。。。。看不懂。。。。char和gets()一般有什么用。。。。
c:\documents and settings\administrator\my documents\visual studio 2008\projects\24asfsaf\24asfsaf\erfa.cpp(11) : error C2679: 二进制“>>”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)
运行时候会看到这个。。。一直是这样。。。。总是会出现这个。。。。
请问这是什么情况???
你看一下你的头文件包含路径设置是不是对,也许你混合了两个不同的STL库的文件。
否则,这个程序是对的。
不要相信RP问题,编程没有RP,只有逻辑。
1