相关疑难解决方法(0)

为什么 getline 函数不能在具有结构数组的 for 循环中多次工作?

我有一个小问题。我创建了一个程序,要求用户输入四个不同零件的零件名称和零件价格。每个名称和价格都填充一个结构,我有一个包含四个结构的数组。当我执行 for 循环来填充所有名称和价格时,我的 getline 函数无法正常工作,它只是在我输入第一部分的名称后跳过输入部分。你能告诉我为什么吗?这是我的代码:

#include <iostream>
#include <string>

struct part {
    std::string name;
    double cost;
};

int main() {

    const int size = 4;

    part apart[size];

    for (int i = 0; i < size; i++) {
        std::cout << "Enter the name of part ? " << i + 1 << ": ";
        getline(std::cin,apart[i].name);
        std::cout << "Enter the price of '" << apart[i].name << "': ";
        std::cin >> apart[i].cost;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays string for-loop getline

3
推荐指数
1
解决办法
2636
查看次数

跨越字符串的初始化并跳转到标签大小写

我正在尝试为我的程序做案例菜单。我总是收到交叉初始化错误,我以前从未见过这个错误。也许有人可以向我解释我的代码出了什么问题。

#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>

using namespace std;

#define N_CARS 1

struct car{
    string model;
    int year;
    double price;
    bool available;
}cars [N_CARS];


void menu();
void mainMenu();

void writeToFile(ofstream &outputFile , const car& p)
{
    outputFile << p.model << " "
               << p.year << " "
               << p.price << " "
               << p.available<<"\n";
}

int choice1 = 0;


int main(int argc, char** argv) {

    menu();
    return 0;
}


void menu() {

    do …
Run Code Online (Sandbox Code Playgroud)

c++ menu switch-statement

3
推荐指数
1
解决办法
1万
查看次数

如何摆脱c ++中的空白行?

我正在尝试填写文本文档,但是当我这样做时 - 我得到第一行空白.我正在阅读的文本文档如下所示:

3
first fr random 5
second          9
third shazam    2
Run Code Online (Sandbox Code Playgroud)

我已经尝试删除第一个值,所以空白行消失了.所以它相关,但我需要这个价值.

// My code
ifstream in("U2.txt"); 
ofstream out("U2.txt");
int n;
in>>n;
char vardaiStart[n][21];

in.read(vardaiStart[0], 21);

out << vardaiStart[0]<< endl;
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样:

*blank*
first fr random
Run Code Online (Sandbox Code Playgroud)

但我想要的是:

first fr random
Run Code Online (Sandbox Code Playgroud)

所以,在其他项目中,我从现在开始不会使用旧的C,但对于这个项目,我更加[endline]并忽略它,如下所示: in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

c++ blank-line

2
推荐指数
1
解决办法
122
查看次数

c ++中getline输入错误

嗨,伙计们,我在从 getline 获取输入时遇到未知错误。我的目的是从用户那里获取一个数字和两个字符串作为输入并打印第一个字符串。这是问题代码

#include <iostream>
using namespace std;

int main() {
  int t;
  cin>>t;
  while(t--)
   {    string s,p;
        getline(cin,s);
        getline(cin,p);
        cout<<s;
   }
  return 0;
  }
Run Code Online (Sandbox Code Playgroud)

现在,当我提供如下输入时:

1
abababa abb
b
Run Code Online (Sandbox Code Playgroud)

它不打印任何东西。为什么会这样?

c++ getline

1
推荐指数
1
解决办法
8131
查看次数

cin >>不能使用getline()

#include <iostream>
#include <string>
using namespace std;

int main () {
  string str;
  int age;
  cout << "Please enter age: ";
  cin>>age;
  cout << "Please enter full name: ";
  getline (cin,str);
  cout << "Thank you, " << str << ".\n";
}
Run Code Online (Sandbox Code Playgroud)

当我使用uperator >>输入整数时,为什么函数getline()不起作用?int输入有什么更好的用途?

c++ cin getline

0
推荐指数
1
解决办法
1919
查看次数

标签 统计

c++ ×5

getline ×3

arrays ×1

blank-line ×1

cin ×1

for-loop ×1

menu ×1

string ×1

switch-statement ×1