You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++计算总分写入文件时出现垃圾值问题求助

Troubleshooting Garbage Characters in Your C++ Report Card Program

Alright, let's figure out why you're seeing random garbage characters when calculating and writing the total score to your file. Your code has several key issues with string handling, input parsing, and buffer management that are causing this problem—let's break them down one by one:

1. Incorrect Input Handling for Marks

In your getdata() function, you're using cin >> m1[0] to read marks, which only captures a single character. This means:

  • If someone enters a two-digit score like 85, only the '8' gets stored in m1[0]
  • Worse, your m1-m5 character arrays don't get a null terminator (\0). When you use strcat later to build the buffer, the function will keep reading memory until it hits a random \0, pulling in garbage characters.

Fix:
Read the marks as integers first, then convert them to properly terminated strings using sprintf (or safer C++ alternatives like std::stringstream):

void report::getdata() {
    cout << "ENTER THE ROLL NO OF THE STUDENT:\t";
    cin >> rollno;
    cout << "ENTER THE NAME OF THE STUDENT:\t";
    cin >> name;
    cout << "ENTER THE MARKS OF THE FOLLOWING SUBJECTS OUT OF 100\n";
    
    int temp;
    // Operation Research
    cout << "OPERATION RESEARCH:\t";
    cin >> temp;
    sprintf(m1, "%d", temp);
    a = temp;

    // Cryptography
    cout << "CRYPTOGRAPHY:\t";
    cin >> temp;
    sprintf(m2, "%d", temp);
    b = temp;

    // Operating Systems
    cout << "OPERATING SYSTEMS:\t";
    cin >> temp;
    sprintf(m3, "%d", temp);
    c = temp;

    // File Structure
    cout << "FILE STRUCTURE:\t";
    cin >> temp;
    sprintf(m4, "%d", temp);
    d = temp;

    // Software Testing
    cout << "SOFTWARE TESTING:\t";
    cin >> temp;
    sprintf(m5, "%d", temp);
    e = temp;

    // Calculate total and convert to string
    s = a + b + c + d + e;
    sprintf(total, "%d", s);
}

2. Broken Total Score Conversion

Your current code for the total score only works if the total is a single digit:

total[0] = s + '0';

If the total is greater than 9 (e.g., 450 for five 90s), s + '0' will result in an ASCII value outside the digit range, producing garbage characters. Plus, you're not adding a null terminator to total, so strcat will again read random memory.

Fix:
Use sprintf (or std::stringstream) to convert the integer total to a properly formatted, null-terminated string—this handles all digit lengths automatically.

3. Bug in unpack() Function

There's a typo in your tokenization logic that breaks field parsing:

p = strtok(NULL, "| "); // Extra space in delimiter

This tells strtok to split on either | or space, which will incorrectly split your data and cause subsequent fields (like m3, total) to read garbage values.

Fix:
Use a consistent delimiter of just | for all tokenization steps:

void report::unpack() {
    char *p;
    p = strtok(buffer, "|");
    strcpy(rollno, p);
    p = strtok(NULL, "|");
    strcpy(name, p);
    p = strtok(NULL, "|");
    strcpy(m1, p);
    p = strtok(NULL, "|");
    strcpy(m2, p);
    p = strtok(NULL, "|"); // Removed extra space
    strcpy(m3, p);
    p = strtok(NULL, "|");
    strcpy(m4, p);
    p = strtok(NULL, "|");
    strcpy(m5, p);
    p = strtok(NULL, "|");
    strcpy(total, p);
}

4. Buffer Overflow Risk

Your buffer is defined as char buffer[SIZE + 1] with SIZE = 55. If the combined length of the student's roll number, name, marks, and total exceeds 55 characters, strcat will overflow the buffer, writing random memory content to your file.

Fix:
Increase the buffer size to something more realistic, like 256:

#define SIZE 256
char buffer[SIZE + 1];

For even safer handling, consider using std::string instead of raw character arrays to avoid manual memory management.

5. Outdated C++ Headers

You're using old, non-standard headers like <iostream.h> and <fstream.h>. These are deprecated and can cause compatibility issues. Switch to standard C++ headers:

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <conio.h> // Note: conio.h is non-standard, works only on Windows
using namespace std; // To avoid typing std:: everywhere

After applying these fixes, your program should correctly write and read the total score without garbage characters.

内容的提问来源于stack exchange,提问作者Rathan D Rao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 19:12:43