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

Turbo C++中二维字符串数组成员函数显示异常求助

Fixes for Your Turbo C++ String Display & Indexing Issues

Let's walk through the bugs in your code and fix them one by one to get your algorithm strings displaying correctly and the agno counter working as expected:

Key Issues & Fixes

1. Wrong Array Index in algout()

When you add an algorithm, agno increments after storing the string. So if you've added 1 algorithm, agno is 1—but your array starts at index 0. Accessing algo[agno] means you're trying to print an uninitialized element (since you only stored data at algo[0]).

Fix: Change cout<<algo[agno]; to cout<<algo[agno - 1];

2. Spurious Semicolon Breaking algout() Logic

The line if(agno>0&&agno<10); has an extra semicolon at the end. This turns the if into an empty statement, so the code inside the curly braces runs every time—even when agno is 0.

Fix: Remove the trailing semicolon: if(agno>0&&agno<10)

3. Input Buffer Garbage Breaking algoin()

After using cin>>opt2, the newline character you typed to confirm is left in the input buffer. The next gets() call will read this newline as an empty string instead of waiting for your input.

Fix: Add cin.ignore(); right after cin>>opt2; to clear the buffer.

4. Broken Loop Condition in algoin()

The condition islower(opt2)=='y' is incorrect. islower() returns a non-zero integer if the character is lowercase, not the character itself. This means your loop will never behave as expected.

Fix: Change the loop condition to while(opt2 == 'y' || opt2 == 'Y') (to accept both lowercase and uppercase 'Y').

Fixed Code Snippets

Updated algoin():

void algoin() {
    clrscr();
    do {
        if(agno < 10) {
            cout<<"\n\nShuffle Enter Algorithm:\n";
            gets(algo[agno]);
            agno++;
            cout<<"\nAlgorithm has been entered";
            cout<<"\nDo you want to add another?<y/n>\n>> ";
            cin>>opt2;
            cin.ignore(); // Clear leftover newline from cin
        } else {
            cout<<"\n\nShuffle Algorithm Capacity is full" <<"\n\nPress any key to continue";
            getch();
            break; // Exit loop once capacity is full
        }
    } while(opt2 == 'y' || opt2 == 'Y');
}

Updated algout():

void algout() {
    clrscr();
    if(agno == 0) {
        cout<<"\n\nNo Shuffle Algorithms present" <<"\n\nPress any key to continue";
        getch(); // Add getch() here so user can see the message before returning
        return;
    }
    if(agno > 0 && agno <= 10) { // Adjusted condition to include max capacity
        cout<<"\n\nShuffle Algorithm:\n ";
        cout<<algo[agno - 1]; // Use correct index
        agno--; // Decrement after printing
        cout<<"\n\nPress any key to continue";
        getch();
    }
}

Extra Notes for Turbo C++

  • Turbo C++ is old, so make sure your algo array is properly declared (e.g., char algo[10][256];—replace 256 with your desired max string length).
  • gets() is unsafe (it doesn't check buffer bounds), but since you're stuck with Turbo C++, just ensure your array size is large enough for user input.

内容的提问来源于stack exchange,提问作者Nilay G

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:49:08