Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 1282|回复: 6
打印 上一主题 下一主题

space 的问题

[复制链接]

23

主题

0

好友

122

积分

高级会员

Rank: 3Rank: 3Rank: 3

跳转到指定楼层
1#
发表于 2009-12-17 10:09 PM |只看该作者 |倒序浏览
各位我有些问题,当我run这个program的时候 我需要填写三个资料,但是当我填写第一个资料的时候有空格的话,它就直接跳去第三个资料,不给我填写第二个资料!

第二个问题是因为我第三个资料是double的但是当我填写 character的时候,它就会一直loop

请问如何解决:

以下是我的file谢谢


cda.h


# include <iostream>
# include <string>

using namespace std;

class CD{
      
      public:
      CD();
      void set_values(string="");
      void set_values(string="",string="");
      void set_values(string="",string="",double=0);
      string get_cd_name();
      string get_artist_name();
      double get_cost();
      void display();
   
      
      private:
      
      string cd_name;
      string artist_name;
      double cost;
      
      } ;




收藏收藏0

23

主题

0

好友

122

积分

高级会员

Rank: 3Rank: 3Rank: 3

2#
发表于 2009-12-17 10:10 PM |只看该作者
cda.cpp

# include "cda.h"
# include <iostream>
# include <string>


using namespace std;

CD::CD(){
         
         cd_name="";
         artist_name="";
         cost=0;
         
         }
         
void CD::set_values(string Cname){
     
     cd_name=Cname;
     
     }
     
void CD::set_values(string Cname,string Aname){
     
     cd_name=Cname;
     artist_name=Aname;
     }

void CD::set_values(string Cname,string Aname,double co){
     
     cd_name=Cname;
     artist_name=Aname;
     cost=co;
     }


     
void CD::display(){
     
     cout <<"Artist:" << artist_name << endl;
     cout <<"CD:" << cd_name << endl;
     cout <<"Cost:" << cost << endl;
     
     }


回复

使用道具 举报

23

主题

0

好友

122

积分

高级会员

Rank: 3Rank: 3Rank: 3

3#
发表于 2009-12-17 10:10 PM |只看该作者
main.cpp

#include <iostream>
#include "cda.cpp"
#include <string>



using namespace std;


int main(){
   
    int i=0,j=0;
    char choice;
    string name;
    string cdname;
    double money;
    CD myCd[100];
    cout <<"*********************\n";
    cout <<"This is the program \nto let you store \nthe CD information";
    cout <<"\n*********************\n\n";
   
    cout <<"1.Store the CD information\n";
    cout <<"2.Display the CD information\n";
    cout <<"3.Quit the program\n";
   
    cin >> choice;
   
   
   
   while (choice !='3'){
         switch(choice){
                 case '1':
                       cout << "Please input the information of the following" << endl;
                       cout <<"Artist name:";
                       cin >> name;
                       cout <<"CD name:";
                       cin >> cdname;
                       cout <<"Cost:";
                       cin >> money;
                       myCd[i++].set_values(name,cdname,money);
                       system("cls");
                       cout << "You already enter " << i << " CD's information.\n" << endl;
                       break;
                      case '2':
                                   for(j = 0; j < i; j++)
                             {     
                               cout << "   Details of" << "CD #" << ++j << ":" << endl;
                                               myCd[--j].display();
                               }
                                break;
                     
                   }
                  
    cout <<"1.store the CD information\n";
    cout <<"2.display the CD information\n";
    cout <<"3.quit\n";
    cin >> choice;
   
   
   
   
}

    cout << "Thanks to use this program" << endl;
    system("pause");
    return 0;
    }


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

4#
发表于 2009-12-17 10:40 PM |只看该作者
原帖由 狂天使 于 2009-12-17 10:10 PM 发表
main.cpp

#include
#include "cda.cpp"
#include



using namespace std;


int main(){
   
    int i=0,j=0;
    char choice;
    string name;
    string cdname;
    double money ...



改用 cin.get 來取得使用者所輸入的字串吧


回复

使用道具 举报

23

主题

0

好友

122

积分

高级会员

Rank: 3Rank: 3Rank: 3

5#
发表于 2009-12-17 11:54 PM |只看该作者
我试了,
但是当我选择1在input 1的时候,他会一直loop 然后 error


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

6#
发表于 2009-12-18 09:22 AM |只看该作者
原帖由 狂天使 于 2009-12-17 11:54 PM 发表
我试了,
但是当我选择1在input 1的时候,他会一直loop 然后 error



1. 使用 char 型態
2. cin.getline(name, 256, '\n')

p/s: 你會出現 error 最主要是初始  string name


回复

使用道具 举报

23

主题

0

好友

122

积分

高级会员

Rank: 3Rank: 3Rank: 3

7#
发表于 2009-12-18 02:32 PM |只看该作者
可以了,我使用这样的方法

                        name="";  
                       while(name=="")   
                         getline(cin,name);

然后就可以输入空格了!!

谢谢


回复

使用道具 举报

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

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-26 02:26 AM , Processed in 0.124151 second(s), 26 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部