调试断言失败 - std :: getline

时间:2014-01-17 01:33:40

标签: c++

std::getline(std::cin, time[d]);

我对此很新,这是我第一次遇到编译后错误。

我认为该错误与此行隔离,但我不太确定。

#include "stdafx.h"
#include <string>
#include <iostream>
#include <limits>
#include <stdlib.h>
#include <vector>
#include <cstdlib>

int main()
{
std::vector<std::string> time(24);
size_t d;
int a;
restart: 

std::cout << "what time of day would you like to write the reflection? (0-23)" <<            std::endl;
std::cin >> d;

if (d > 23)
{
    std::cout << "Please enter a time of day between 0 - 23" << std::endl;

    system("PAUSE"); 
    system("CLS");
    goto restart;
}
else 

std::cout << "What would you like to have inside of that?" << std::endl;
std::cout << "Please enter a sentence: ";
std::getline(std::cin, time[d]);

std::cout << "Is this what you wrote?" << std::endl;
std::cout << "[" << time[d] << "]" << std::endl;

system("pause");

std::cout << "To add a new reflection press 1" << std::endl;
std::cout << "To output all of the reflections press 2" << std::endl;
std::cout << "To terminate program press 3" << std::endl;
std::cin >> a; 

if (a == 1)
{
    goto restart;
}
if (a == 2)
{
    system("pause");

    std::cout << time[0] << std::endl;
    std::cout << time[1] << std::endl;
    std::cout << time[2] << std::endl;
    std::cout << time[3] << std::endl;
    std::cout << time[4] << std::endl;
    std::cout << time[5] << std::endl;
    std::cout << time[6] << std::endl;
    std::cout << time[7] << std::endl;
    std::cout << time[8] << std::endl;
    std::cout << time[9] << std::endl;
    std::cout << time[10] << std::endl;
    std::cout << time[11] << std::endl;
    std::cout << time[12] << std::endl;
    std::cout << time[13] << std::endl;
    std::cout << time[14] << std::endl;
    std::cout << time[15] << std::endl;
    std::cout << time[16] << std::endl;
    std::cout << time[17] << std::endl;
    std::cout << time[18] << std::endl;
    std::cout << time[19] << std::endl;
    std::cout << time[20] << std::endl;
    std::cout << time[21] << std::endl;
    std::cout << time[22] << std::endl;
    std::cout << time[23] << std::endl;

    system("pause");
}
else
    system("pause");
return 0;

}

1 个答案:

答案 0 :(得分:1)

您只声明了time的{​​{1}}。所以你只能打印它的第一个内容。

要使其正常工作,您可以添加类似

的内容
size=1

使其足以容纳内容。

或者您需要删除:

time.resize(24); // min:24, as you print time[23] later