【问题标题】:C++ string function error and Print [closed]C++字符串函数错误和打印[关闭]
【发布时间】:2014-10-19 16:58:11
【问题描述】:

所以我正在做这个项目,要求用户根据需要在系统中输入几乎尽可能多的客户。他们应该输入客户姓名、电影、每件商品的数量和成本。稍后它应该提供总和、平均值等(平均值等我还没有实现)。

更新:

我已经修复了我的字符串,但我认为我的打印数组有问题。不完全确定如何将我的名字变成一个字符串,这样我就可以保留 Alyssa 和 Dave 错误只是一堆数字,只是继续下去:

程序正在运行

Welcome to our storing system for your account information
If you wish to begin inputting information, Please enter 1 for Yes and 2 for No
1
Enter customer's first name:
Alyssa
To choose which Motion Picture that you purchased a DVD under:
Enter 1 for Paramount. Enter 2 for Twentieth Century Fox.
Enter 3 for Warner and 4 for WaterFront

Input the data in the following format.
Motion Picture Number <space> Amount of motion pictures purchased <space>  Cost of movies
Enter -1 <space> 0 <space> 0 <space> to end

1 2 20
Enter Motion Picture <space> Amount purchased <space> Cost of movies
2 33 201
Enter Motion Picture <space> Amount purchased <space> Cost of movies
-1 0 0
Still have more employees? Enter 1 to add them or 2 to end

1
Enter customer's first name:
Dave
To choose which Motion Picture that you purchased a DVD under:
Enter 1 for Paramount. Enter 2 for Twentieth Century Fox.
Enter 3 for Warner and 4 for WaterFront

Input the data in the following format.
Motion Picture Number <space> Amount of motion pictures purchased <space>  Cost of movies
Enter -1 <space> 0 <space> 0 <space> to end

1 4 20
Enter Motion Picture <space> Amount purchased <space> Cost of movies
-1 0 0
Still have more employees? Enter 1 to add them or 2 to end

2

错误

下面列出的是您在每个电影公司中的总数

客户名称派拉蒙二十世纪福克斯华纳海滨 戴夫0 戴夫20 戴夫0 戴夫20 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0 戴夫0戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫 0 戴夫

我想要的印刷品:**

 Customer Name    Paramount   Twentieth Century Fox    Warner    WaterFront   Total

 Alyssa              2               4                   0            0        80
  Dave               3               0                   0            0       66.00

**

等等

MotionPicture.h

#ifndef MOTIONPICTURE_H
#define MOTIONPICTURE_H
#include <cstring>

using namespace std;

class motionPicture {


    public:

    motionPicture();
    void getData();
    static const int rows = 6;
    static const int columns = 100;
    void printArray();
    void getName();
    void setName();


    private:
            int company;
            double table[rows][columns];
            string name;

 };

 #endif

MotionPicture.cpp

 #include <iostream>
 #include "motionPicture.h"
 #include <iomanip> //to use setw
 #include <cstring>
 #include <string>

 using namespace std;

 motionPicture :: motionPicture() {

    //initalize the table
    for (int i =0; i < rows; i++){

            for (int j=0; j<columns; j++){

                    table[i][j] =0;
            }//end j for loop
    }//end i for loop

}//end constructor

void motionPicture :: setName(){

    cout<<"Enter customer's first name: " <<endl;
    cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
    getline(cin, name);
}//end setName

void motionPicture :: getName() {
    cout<< name;
//}//end getName

void motionPicture:: getData() {

    int system;
    int motion, quanity;
    double cost;

    cout <<"If you wish to begin inputting information, Please enter 1 for Yes and 2 for No"<<en$
    cin >> system;

    while (system !=2)
    {
    setName();
    //getName();

    cout <<"To choose which Motion Picture that you purchased a DVD under:"<<endl;
    cout <<"Enter 1 for Paramount. Enter 2 for Twentieth Century Fox."<<endl;
    cout <<"Enter 3 for Warner and 4 for WaterFront"<<endl<<endl;

    cout <<"Input the data in the following format."<<endl;
    cout <<"Motion Picture Number <space> Amount of motion pictures purchased <space>  Cost of m$
    cout <<"Enter -1 <space> 0 <space> 0 <space> to end"<<endl<<endl;

    cin >> motion >> quanity >> cost;

    while (motion != -1) {

                    table[motion-1][quanity-1] +=cost;

                    cout <<"Enter Motion Picture <space> Amount purchased <space> Cost of movies$
                    cin >> motion>> quanity >>cost;
            }//end cost while
    cout<<"Still have more employees? Enter 1 to add them or 2 to end" <<endl<<endl;
    cin >>system;
    }//end system while
}//end getData


void motionPicture :: printArray() {

cout <<"Listed below is your totals within each Motion Picture Company"<<endl;
cout <<"----------------------------------------------------------------"<<endl;

cout<<"Customer Name" <<setw(15) << "Paramount" <<setw(11) << "Twentieth Century Fox"
    << setw(23)<<"Warner" <<setw(8) <<"WaterFront" << setw(12)<<endl;
    for (int i= 0; i <rows; i++){
            //for(int j=0; j<100; j++){
            double total =0;
               // begin to print the table
               for (int j=0; j<columns; j++) {
                getData();
               cout table[i][j] << " ";
            //input total
            total += table[i][j];
            }//end j loop
    cout <<total<<"\n";
    }//end i loop
 }//end print

motionRun.cpp

 #include <iostream>
 #include "motionPicture.h"


 using namespace std;

 int main () {

    cout << "Welcome to our storing system for your account information"
    <<endl;

    motionPicture objMotion;
    objMotion.getData();
    objMotion.printArray();

 return 0;
 }

【问题讨论】:

  • 编译器已经说明出了什么问题,在哪个文件中,在哪一行
  • 我知道错误的位置。我不熟悉错误的含义。

标签: c++ arrays string printing


【解决方案1】:

你必须写

std::cin.getline(string1,20,'\n');

而不是

getline(string1,20,'\n');

在函数定义中。

你必须定义静态数据成员

char static string1[];

在 cpp 模块中。

也不清楚这行在构造函数中的含义

string1[20];

【讨论】:

  • 产生更多错误:/tmp/ccTuXxxl.o:在函数motionPicture::getName()': motionPicture.cpp:(.text+0x149): undefined reference to motionPicture::string1' /tmp/ccTuXxxl.o:在函数motionPicture::printArray()': motionPicture.cpp:(.text+0x2c0): undefined reference to motionPicture::string1'/tmp/ ccTuXxxl.o: 在函数motionPicture::setName()': motionPicture.cpp:(.text+0x3bc): undefined reference to motionPicture::string1' collect2: ld 返回 1 退出状态
  • @Alyssa Cooke 查看我更新的帖子。您必须定义静态数据成员。
  • @AlyssaCooke 在 cpp 文件中使用 char motionPicture ::string1[20] ;
  • 到目前为止,我已将变量更改为字符串 string1[]; ...我也从我听到的消息中意识到我仍然需要我的 cin>>userInput;在我使用 getline 等之前。现在还在玩。试图弄清楚如何获取输入并将其放入我的 string1 中。
【解决方案2】:

你的班级有一个隐藏的问题:

您拥有私人会员:

       char static string1[];   // no space was reserved for this string !! 

在构造函数中,您尝试通过编写来设置大小:

string1[20];   // this is only an expression returning the 20th element of string1.  

但这行不通。字符串不会被重新分配,当你在其中存储一些东西时,你会遇到一个错误(运气好一点是分段错误,但它也可能不会被注意到)。

我不知道你是否真的希望字符串是static,这意味着你的类的所有实例都共享...

解决问题的最佳方法是将字符串定义为真正的 c++ 字符串:

   string string1;   // requires #include<string> of course

这将是一个真正的动态字符串。不再需要担心它的大小。您的getline() 将按原样工作。

【讨论】:

  • 我在上面的评论:到目前为止,我已将变量更改为字符串 string1[]; ...我也从我听到的消息中意识到我仍然需要我的 cin>>userInput;在我使用 getline 等之前。现在还在玩。试图弄清楚如何获取输入并将其放入我的 string1 中。
  • 如果定义为字符串则不使用[]。
猜你喜欢
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多