C++ 单词生成器问题

标签 c++ string cin

我目前正在编写一个程序,使用一个函数将歌词混在一起并将其转换为单词数组,从而用歌词制作游戏。我当前的问题是,当我使用普通字符串运行程序时,它工作正常,但是当我尝试使用 cin 调试程序以将用户输入保存到字符串时,它在我输入用户输入后立即退出且没有错误.我已将程序简化为它实际上是什么的骨架,这样就不会混淆问题出在哪里。任何帮助将不胜感激。

#include <iostream>
#include <fstream>
#include <istream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <array>
#include <cctype>
#include <cstdlib>

using namespace std;

//function that gets the characters from a string and separates them into words 
//then saves them    in a vector
vector <string> getlyrics(string dat)
{
    //save dat as a new string data
    //get the size of the data string to get number of characters
    string data = dat;
    int siz = data.size();
    //used to keep track of current letter position when creating words
    string letter;
    //used to keep track of previous letter position when creating words
    string prevletter;
    //used to keep track of current word
    string word;
    //creates a vector called lyrics to keep track of lyrics in order
    vector <string> lyrics;

    //for every letter in data string...
    for(int i = 0; i<=siz; i++)
    {
        //set the letter for current place in data string
        letter+=data[i];

        //if we are on the first letter...
        if(i == 0)
        {
            //... then the previous letter is empty
            prevletter = "";
        }
        //otherwise, the previous letter is the previous character in data string
        else
        {
            prevletter += data[i-1];
        }

        //if the letter is not equal to a space or a newline or an empty...
        if(letter != " " && letter != "\n" && letter != "")
        {
            //then add the current letter to the word
            word += data[i];
            //reset the letter
            letter = "";

        }
        //otherwise if the letter is a space or newline
        else if(letter== " " || letter == "\n" || letter == "")
        {
            //if the previous letter not equal to a space or newline
            if(prevletter!=" "||prevletter!="\n")
            {
                //then the word is finished
                //add it to the lyrics vector without adding the new letter to the word
                lyrics.push_back(word);
                //reset the word, letter, and add 1 to the word count
                word = "";
                letter = "";
            }
        }
        //reset the previous letter
        prevletter = "";
    }
    //return the vector lyrics
    return lyrics;
}
int main()
{
    //creates a user input string called text
    string textstr;
    cin>>textstr;
    //runs the string through getlyrics() function and saves it in vector called textvec
    vector <string> textvec = getlyrics(textstr);
    //creates a string array called textarr of equal size to the vector
    string textarr[textvec.size()];
    int i = 0;
    //for each value in the vector, save that value to a corresponding index in textarr
    for(auto c : textvec)
    {
        textarr[i] = c;
        i++;
    }
    //print out all values stored in textarr
    //(I can use the size of textvec as the max because textvec and textarr are equally sized)
    for(int k = 0; k<textvec.size(); k++)
    {
        cout<<textarr[k]<<endl;
    }
    //wait for user input
    int x;
    cin>> x;
}

最佳答案

你的根本问题在这里:

string textstr;
cin>>textstr;

当您以这种方式读取字符串时,它会在第一个空格处停止读取输入,因此 textstr 将仅包含您键入的第一个“单词”1。您可能想改用 std::getline


<支持> 1. 这可以用来编写一个更简单的 getlyrics 函数。

关于C++ 单词生成器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27261738/

相关文章:

c++ - 如果用户在int中输入了char,我该如何使用输入?

C++ getline inside while loop 缺少第一个字母。没有 cin.ignore 不工作?

c++ - 完整对象还是子对象?

windows - 使用批处理脚本将命令输出到文件

python - 给定数字的最小排列

c - 以下程序如何处理指针操作

c++ - 从 ifstream 读取 stdin/cin 是否安全?

c++ - 如何将 std::stringbuf 转换为 char 数组?

c++ - 为什么 'nullptr' 命名空间中没有 'std'?

c++ - 我可以使用枚举作为结构名称吗?