繁体   English   中英

C++程序中的意外输出

[英]unexpected output in c++ program

我正在一个研讨会上工作,我已经完成了它,但我在调试时遇到了麻烦。

该程序编译并运行,但我遇到了一个问题,在要求用户输入数据并发送到显示功能后,它显示垃圾。

我已经通过调试模式运行了程序,我得出的结论是输入没有传递给我的 setter 函数,但是在我教授写的代码中(他写了主要内容并要求我们填写一些内容,例如内存分配)它不要求我在 main 中初始化 setter 函数,我错过了什么吗?

'Weather.h' 文件是集合和显示函数在 Weather 类中的位置。

#include <iostream>
#include "Weather.h"
using namespace std;
using namespace sict;
int main(){
  int n; //the count of days worth of weather
  // initialize the weather pointer here
  Weather* weather;

  cout << "Weather Data\n";
  cout << "=====================" << endl;
  cout << "Days of Weather: ";
  cin >> n;
  cin.ignore();
  // allocate dynamic memory here
  weather = new Weather[n];

  for (int i = 0; i < n; i++){
    char date_description[7];
    double high = 0.0, low = 0.0;

    // ... add code to accept user input for
    //weather
    cout << "Enter date: ";
    cin >> date_description;
    cout << "Enter high: ";
    cin >> high;
    cout << "Enter low: ";
    cin >> low;
  }
  cout << endl;
  cout << "Weather report:\n";
  cout << "======================" << endl;

  for (int i = 0; i < n; i++){
    weather[i].display();
  }

  // deallocate dynamic memory here
  delete[] weather;
  weather = (Weather*)0;

  return 0;

}
/*
Output Example :
Weather Data
== == == == == == == == == == =
Days of Weather : 3
Enter date : Oct / 1
Enter high : 15
Enter low : 10
Enter date : Nov / 13
Enter high : 10
Enter low : 1.1
Enter date : Dec / 15
Enter high : 5.5
Enter low : -6.5

Weather report :
== == == == == == == == == == ==
Oct / 1_______15.0__10.0
Nov / 13______10.0___1.1
Dec / 15_______5.5__ - 6.5
*/

set函数的定义(在天气中):

void Weather::set(const char* Date, double high, double low){
        strcpy(date, Date);
        tempHigh = high;
        tempLow = low;
    }

您读取数据并在weather = new Weather[n];之后将它们放入for 循环中weather = new Weather[n]; .
您将不得不将它们储存起来以备weather 应该可以这样做:

  for (int i = 0; i < n; i++){
    const int store_length = 7;
    char date_description[16];
    char date_description_to_store[store_length];
    int store_pos = 0;
    double high = 0.0, low = 0.0;

    // ... add code to accept user input for
    //weather
    cout << "Enter date: ";
    cin.getline(date_description, sizeof(date_description) / sizeof(date_description[0]));
    for (int i = 0; date_description[i] != '\0' && store_pos < store_length - 1; i++){
      if (date_description[i] != ' ') date_description_to_store[store_pos++] = date_description[i];
    }
    date_description_to_store[store_pos] = '\0';
    cout << "Enter high: ";
    cin >> high;
    cout << "Enter low: ";
    cin >> low;
    weather[i].set(date_description_to_store, high, low); // add this line
    cin.ignore(); // add this line to ignore the new line
  }

更新:您应该使用cin.getline读取包含空格的字符串,例如Oct / 1
更新 2: 7 个字符的缓冲区不足以读取Oct / 1 您将不得不分配更多内存或使用std::string
更新 3 :您必须将Oct / 3等输入格式转换为Jan/21等存储格式。 请注意,此代码中没有错误检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM