|
查看: 1151|回复: 6
|
C++ String的問題
[复制链接]
|
|
|
以下是我寫的,可是一直有3個Error,改了很久還是沒有頭緒,所以請各位高手幫幫我解決問題
#include <iostream.h>
#include <string>
using std::string;
using namespace std;
void main()
{
string c1, c2, c3;
int q1, q2, q3;
cout << "Code: ";
cin >> c1;
cout << "Quantity: ";
cin >> q1;
cout << "\nCode: ";
cin >> c2;
cout << "Quantity: ";
cin >> q2;
cout << "\nCode: ";
cin >> c3;
cout << "Quantity: ";
cin >> q3;
} |
|
|
|
|
|
|
|
|
|
|
发表于 4-2-2009 09:42 PM
|
显示全部楼层
我不会用C++的
有些东西可能会错
不过以我的经验来讲
你的string放少了一样东西
你试试放一个东西
=" " (在java里头,C++ 麻就不知道了)
终只在string里头是不能没有value
一定要放,不然就放 =" "
你试试吧,要不然就就看它写些什么error 吧 |
|
|
|
|
|
|
|
|
|
|
发表于 4-2-2009 09:53 PM
|
显示全部楼层
为什么不直接讲你遇到什么error....你以为每个人都有义务帮你检查每一行源代码吗?
cin 好像不支持 int 的 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 4-2-2009 10:04 PM
|
显示全部楼层
原帖由 yeenfei 于 4-2-2009 09:53 PM 发表 
为什么不直接讲你遇到什么error....你以为每个人都有义务帮你检查每一行源代码吗?
cin 好像不支持 int 的
C:\Users\Administrator\Documents\test_1.cpp(12) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no
acceptable conversion)
C:\Users\Administrator\Documents\test_1.cpp(17) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no
acceptable conversion)
C:\Users\Administrator\Documents\test_1.cpp(22) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no
acceptable conversion)
Error executing cl.exe.
test_1.cpp.obj - 3 error(s), 0 warning(s)
[ 本帖最后由 wJien 于 4-2-2009 10:06 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 10-2-2009 09:49 AM
|
显示全部楼层
回复 4# wJien 的帖子
因该没什么问题才对, 你用什么 compiler?
试试正规写法。。- #include <iostream>
- #include <string>
- //using std::string;
- //using std::cin;
- //using std::cout;
- using namespace std;
- int main()
- {
- string c1, c2, c3;
- int q1, q2, q3;
- cout << "Code: ";
- cin >> c1;
- cout << "Quantity: ";
- cin >> q1;
- cout << "\nCode: ";
- cin >> c2;
- cout << "Quantity: ";
- cin >> q2;
- cout << "\nCode: ";
- cin >> c3;
- cout << "Quantity: ";
- cin >> q3;
- return 0;
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 10-2-2009 02:43 PM
|
显示全部楼层
|
如果没记错,cin 是不能接受string/character.你不妨把cin 改成cin.getline() or change string to char*. then you also can consider cin become to put_char() as well. |
|
|
|
|
|
|
|
|
|
|
发表于 10-2-2009 05:35 PM
|
显示全部楼层
回复 6# 吉打棋粪 的帖子
1. cin 可以接受任何 numeric / character, 但是不能接受whitespace
2. cin.getline() 用于 c-string, 也就是character array, 不是string
3. string 是用 getline( <input stream > , <variable > ) |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|