我的c++程序在我不告诉他的地方连接字符串
My c++ program concatenates strings where i dont tell him to
嗨,我正在为我在c++上的一个家庭作业编写这个小程序。它是关于多项式的,它要求多项式的次数、指数、系数和常数来乘以系数。现在我有了我一直在做的代码:
#include <iostream>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
int deg=0, x=0, con, co;
string m, n, poli="";
ostringstream convert, convert1;
cout<<"Hi! this program will show the resulting polynomial of multiplying the npolynomial times the constant.nn";
cout<<" ";
cout<<"nnBut first give me the degree of the polynomial (max 9): "; cin>>deg;
cout<<"nnNow the Multiplier constant: "; cin>>con;
cout<<"nnNow give me the coefficients of the polynomial.n";
while(deg!=0){
int n1=0, n2=0, n3=0;
string m="", n="";
convert.clear();
convert1.clear();
cout<<"Exponent " << deg <<" coefficient: "; cin >>co; cout<<"nn";
if(co!=0){
n2=(co*con);
n3=(deg+0);
convert << n2;
convert1 << n3;
m = convert.str();
n = convert1.str();
poli+=m+"x^"+n+" ";
cout<<poli+"nn";
}
deg--;
}
}
但出于某种原因,比如说3x^4 2x^3 2x2
... it out puts
3x^4 32x^43 322x^432
正如你所看到的,它是由于某种原因连接在一起的,我不知道为什么,你们能帮我吗?
您似乎误解了std::basic_ios::clear`函数的作用。它不清除流缓冲区。这意味着每次你做
convert << n2;
convert1 << n3;
您正在附加到流中。
要解决此问题,请将流变量convert
和convert1
设置为循环内部的本地变量,就像m
和n
一样。
我已经声明了'ostingstream convert,convert1;'在while循环中,它在这里工作得很好。每次while循环迭代后,流都没有清除。
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- 在 libcurl 连接池中预创建连接
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- 对等方断开连接后未释放 SSL 内存
- 发送大型数据时由对等方重置 Tcp 连接
- 使用 c++ 为大至 1000 x 1000 的矩阵实现 8 连接连接的组件标记
- Flash客户端c++服务器连接被对等方重置
- 头可连接连接是否可以具有争用条件?你如何绕过它