質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

1139閲覧

C++でスタックが満杯時に何も出力されない

crew_4820

総合スコア1

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2021/10/14 01:49

編集2021/10/14 01:59

前提・実現したいこと

C++を使い、文字列要素を格納するプログラムを書いています。

発生している問題・エラーメッセージ

エラー文はなく、コンパイルも通るのですが実行の途中で何も出力されずに終わってしまいます。

該当のソースコード

c++

1//stack.h 2#ifndef STACK_H 3#define STACK_H 4 5#include <iostream> 6 7// Insert the relevant headers here 8 9class Stack { 10public: 11Stack(int max_size=2) : _max_size(max_size), 12_top(-1), _data(new std::string[max_size]) {} 13 14~Stack() { delete[] _data; } 15 16 // interface 17 18 // check whether the stack is empty 19bool empty(); 20 21 // check whether the stack is full 22bool full(); 23 24 // return the number of elements 25int size(); 26 27 // insert element on top 28 // print an error message on std::cerr when overflow 29void push(std::string); 30 31 // remove element on top 32 // print an error message on std::cerr when underflow 33void pop(); 34 35 // acces the topmost element 36std::string top(); 37 38private: 39 int _max_size; // capacity of the fixed-size stack 40 int _top; // index to the top of the stack 41 std::string* _data; // data container 42 43}; 44#endif
//stack.cpp #include <iostream> #include "stack.h" using namespace std; bool Stack::empty() { if(_top == -1) { return true; } else return false; } bool Stack::full() { if(_top == _max_size) { return true; } else return false; } int Stack::size() { _top = 0; return _top; } void Stack::push(string data) { if(full()){ cout << "Error: Stack is full!" << endl; } else { _data[++_top] = data; } } void Stack::pop() { if(empty()){ cout<<"Error: Stack is empty!" << endl; } else { _data[--_top]; } } string Stack::top() { return _data[_top]; }
//main.cpp #include <iostream> #include "stack.h" using namespace std; int main(void) { Stack stack; stack.push("Good morning!"); stack.push("Hello!"); cout << stack.top() << endl; stack.pop(); cout << stack.top() << endl; stack.pop(); stack.push("Good evening!"); stack.push("Good evening!"); stack.push("Good evening!"); stack.pop(); stack.pop(); cout << stack.top() << endl; return 0; }

試したこと

emptyの時はstack.pop();のところで"Error: Stack is empty!"が出力されることは試しました。
文字列が3つ格納されたときにだけ何も出力されずに実行が終わってしまいます。

下が実行結果です。

Hello!
Good morning!

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

cpp

1bool Stack::full() { 2 if(_top == _max_size) {

_max_sizeが2だったら、満タンのときの _top は1ですよね。_top + 1 == _max_size なんじゃないでしょうか。

投稿2021/10/14 01:58

int32_t

総合スコア20884

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

crew_4820

2021/10/14 02:05

ちゃんと出力されるようになりました! ありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問