我正在尝试使用getline读取一串文本.出于某种原因,它会打印两次"请输入您的选择":
Please enter your selection
Please enter your selection
Run Code Online (Sandbox Code Playgroud)
如果我键入无效文本,它会再次循环,并且此后每次循环只打印一次.
while (valid == false) {
cout << "Please enter your selection" << endl;
getline (cin,selection);
// I have a function here which checks if the string is valid and sets it to true
// if it is valid. This function works fine, so I have not included it here. The while
// look breaks correctly if the user enters valid input.
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道为什么会这样?
谢谢
如果有人问过这个我很抱歉,但我似乎找不到能帮助我的解决方案.我正在尝试从文本文件中读取数据,并最终将其存储在对象中.
文本文件有4个变量,全部用逗号分隔.
我试过这样做如下:
string v1, v2, v3, v4;
ifstream afile;
afile.open("thefile.txt");
afile >> v1 >> v2 >> v3 >> v4;
afile.close();
cout << v1 << endl;
cout << v2 << endl;
cout << v3 << endl;
cout << v4 << endl;
Run Code Online (Sandbox Code Playgroud)
该文件有多个记录.我试图现在只做1以确保它有效,但是当它读入数据时,它不会在逗号中分开.
从那里,我想将数据存储到一个对象中.以下工作:Thing*thing1 = new Thing(v1,v2,v3,v4);
虽然,当我读到5条记录时,构建上述代码行的最佳方法是什么?由于每个对象都需要一个唯一的名称,有没有办法可以使用for循环和向量进行迭代?ie for(int i = 0; i <5; i ++){//读入数据//存储在vector中}
非常感谢任何提示
我试图想出一种方法将2个向量和一个整数合并为一个向量.即
return data.push_back(fn(data1), mid, fn(data2));
Run Code Online (Sandbox Code Playgroud)
NB这是一个递归函数.向量数据在到达return语句之前具有存储在其中的值.我需要使用return语句中的值更新数据中的值.
我完全不知道如何去做这件事.我一直在寻找几个小时,但似乎什么都没有用!
非常感谢任何指导.
我有一个相当长的程序,我正在努力,并且很难从向量中删除元素.我试图用一个非常简单的向量来做,并且遇到了同样的问题.据我所知,我的做法与每个人在其他人的问题中解释的方式相同.这是简单的代码.
vector<int> vect;
vect.push_back(3);
vect.push_back(2);
cout << vect[1]; // prints '2'
vect.erase(vect.begin()+1);
cout << vect[1] << endl; // prints '2'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
看起来上面的代码是有效的,因为我检查了最后的大小,并打印出"1".真正的代码不是:
size = A[i]->B().size();
cout << "size is " << A[i]->B().size() << endl; // prints 21
A[i]->B().erase(A[i]->B().begin()+size);
cout << "size now " << A[i]->B().size() << endl; // prints 21
Run Code Online (Sandbox Code Playgroud)
我看不出我做了什么不同的事情?A是矢量,存储其他矢量.我想删除B向量中的最后一个元素.
我正在尝试使用play()函数设置一个新类.我不确定我做错了什么,因为我有其他课程,我以类似的方式实施,他们工作正常.有人可以指出我可能犯了错误吗?
.h文件
#ifndef GAME_H
#define GAME_H
#include <string>
using namespace std;
class Game {
public:
Game();
void play();
};
#endif
Run Code Online (Sandbox Code Playgroud)
.cpp文件
#include "game.h"
#include <string>
#include <iostream>
using namespace std;
Game::Game() {}
Game::play() {}
Run Code Online (Sandbox Code Playgroud)
我将play功能调用如下:
Game* theGame = new Game();
theGame->play();
Run Code Online (Sandbox Code Playgroud)
我编译时遇到以下错误:
game.cpp:10: error: ISO C++ forbids declaration of ‘play’ with no type
game.cpp:10: error: prototype for ‘int Game::play()’ does not match any in class ‘Game’
game.h:16: error: candidate is: void Game::play()
game.cpp:10: error: ‘int Game::play()’ cannot be overloaded
game.h:16: …Run Code Online (Sandbox Code Playgroud) 我无法理解我遇到的一些错误。我已经构建了一个带有驱动程序的简单测试类。有人可以指出我犯的错误吗?
在这里,我尝试创建一个 Test 对象并将 number 变量设置为 1,然后打印 number 变量。
司机:
#include "test.h"
#include <iostream>
using namespace std;
int main() {
Test *myTest = new Test(1);
cout << myTest->getNumber();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
测试.h
#ifndef __TEST_H__
#define __TEST_H__
class Test
{
private:
int number;
public:
Test();
Test(int theNumber);
int getNumber();
};
#endif
Run Code Online (Sandbox Code Playgroud)
测试.cpp
#include "test.h"
Test() {
}
Test(int aNumber) {
number = aNumber;
}
int getNumber() {
return number;
}
Run Code Online (Sandbox Code Playgroud)
我在这里遇到的错误是
> Undefined symbols for architecture x86_64: "Test::getNumber()",
> referenced from: …Run Code Online (Sandbox Code Playgroud) 非常适合XSL的初学者.我正在尝试将2个XML文档转换为新的XML文档.我似乎无法将标签打印到新文档.只打印元素的内容.
XML
<books>
<book>
<name>Cat in the Hat</name>
<author>Me</name>
...
...
</book>
...
...
</book>
Run Code Online (Sandbox Code Playgroud)
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method = "xml" indent = "yes" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:element name="books">
<xsl:template match="books">
<xsl:element name="book">
<xsl:for-each select="book">
<xsl:element name="name"><xsl:value-of select="name"/></xsl:element>
<xsl:element name="author"><xsl:value-of select="author"/></xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:element>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
输出到XML文件Cat Hat in Hat Me
我需要输出为:
<books>
<book>
<name>Cat in the Hat</name>
<author>Me</author>
</book>
</books>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?