Error while performing string copy operation in C++ - c++

Please explain why is this giving an error but the other on is running fine
The following code gives the error:
#include <iostream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
string s1,s2;
int i;
cout << "Enter the string to copy into another string : ";
getline(cin,s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
cout<<"\n\nCopied String S2 is : "<<s2;
return 0;
}
Error looks like this
But this works perfectly fine
#include <iostream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
char s1[100], s2[100], i;
cout << "Enter the string to copy into another string : ";
cin>>s1;
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
cout<<"\n\nCopied String S2 is : "<<s2;
return 0;
}

In your case, s2 is initialized to an empty string with a length of 0, so you can't write past the bounds. If you want to, you must first resize it:
s2.resize(s1.length());
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
Also, c++ std::string does not need a terminating nullbyte, unlike C strings.

Related

why my cpp code can't run?(about char*[])

this is my code
the error is Segmentation fault,and i can't understand why
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
char* szword[100];
int i = 0;
do
{
cin >> szword[i];
cout << szword[i];
i++;
}while(strcmp(szword[i - 1], "done"));
cout << i + 1;
return 0;
}
For starters neither declaration from headers <cstdio> and <string> is used in your program. So you should remove these directives
#include <cstdio>
#include <string>
You declared an initialized array with the element type char *. Thus this statement
cin >> szword[i];
invokes undefined behavior because the pointer szword[i] has indeterminate value.
Moreover this call even if the argument of the operator will be correct
cin >> szword[i];
can fail. You should check whether it was successful. And I think there is no great sense to output the string "done".
Also in this statement
cout << i + 1;
you are outputting a value that is greater than the number of inputted strings.
If to use character arrays then your program could look the following way
#include <iostream>
#include <cstring>
int main()
{
const size_t N = 100;
char szword[N][N];
size_t i = 0;
while ( std::cin.getline( szword[i], sizeof( szword[i] ) ) &&
std::strcmp( szword[i], "done" ) != 0 )
{
std::cout << szword[i++] << '\n';
}
std::cout << i << '\n';
return 0;
}
The program output might look like
Hello
World
2
This below code works fine, if you want to use char *, for C++ string you can use the C++ version
C Version:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
char *tmp;
int i = 0;
do
{
cin >> tmp;
cout << tmp;
i++;
}while(strcmp(tmp, "done"));
cout << i + 1;
return 0;
}
C++ Version:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
string tmp;
int i = 0;
do
{
cin >> tmp;
cout << tmp;
i++;
}while(tmp != "done"));
cout << i + 1;
return 0;
}

Difficulty understanding while loop execution

#include <iostream>
#include <iomanip>
#include <ios>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
using std::cin; // <iostream>
using std::cout; // <iostream>
using std::endl; // <iostream>
using std::setprecision; // <iomanip>
using std::sort; // <algorithm>
using std::streamsize; // <ios>
using std::string; // <string>
using std::vector; // <string>
int main()
{
cout << "Enter your homework grades : " << endl;
double x;
vector<double> homework;
int count = 0;
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0)
{
cout << "Error, enter a grade" << endl;
continue;
}
}
return 0;
}
Hi, I'm wondering why my while loop wont print the message (Error, enter a grade) on the screen if the if-statement is within the loop, and seems to only work when its placed outside after the loop executes why is this the case?
You are never going back to zero.
int count = 0;
//...
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0) //count will never be zero

Vectors and Strings C++

#include <vector>
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
int main()
{
vector<string> test;
test.push_back("yasir");
test.push_back("javed");
for(int i=0; i!=test.end();i++)
{
cout << test[i];
}
}
Why is this code giving up an error? I am unable to identify the cause of the error.
Error: No Match for operator !=....
First of all, you are trying to compare int with the iterator of vector.
for(int i=0; i!=test.end();i++)
{
cout << test[i];
}
Here, the test.end() returns the iterator. There is no overloaded operator!= which can compare integer (int i = 0) with that iterator (test.end()).
So your loop should look more like:
for (std::vector<string>::iterator i = test.begin(); i != test.end(); i++)
{
cout << *i;
}
You can replace std::vector<string>::iterator with auto, if using C++11 or newer.
The next thing, you included <string.h> which contains old functions such as: strlen, strcpy. Similarly, <cstring> contains C-style strings.
If you want to you use operator<<, so if you want to write:cout << then you have to do: #include <string>.
As already mentioned, the problem is, that you try to compare an integer with an iterator in the "middle" of your for statement. Try this instead, it's more intuitive from my point of view
#include <vector>
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
int main()
{
vector<string> test;
test.push_back("yasir");
test.push_back("javed");
for(int i=0; i<test.size();++i)
{
cout << test[i];
}
}

how can i save a text file without typing ".txt" at the end?

My Program (c++):
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
float x, y, z;
char d[20];
int main()
{
cin.getline >>d;
x=111;
y=222;
z=333;
ofstream meuarquivo;
meuarquivo.open (d".txt");
meuarquivo << x << "\n";
meuarquivo << y << "\n";
meuarquivo << z << "\n";
meuarquivo.close ();
return 0;
}
I want to write something like "ThatsMyProgram", and I want the program to save this file as "ThatsMyProgram.txt". How can I do that?
Use std::string, which defines operator + for concatenation:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main()
{
std::string filename;
std::getline(std::cin, filename);
std::ofstream file((filename + ".txt").c_str());
// use stream here.
}

Printing with fixed spaces

I want to print two strings (say "ABC" and "DEF") with 5 space characters before "ABC" and that the second string will start 7 characters after the beginning of the first string.
I suspect you're looking for the width() method:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string abc = "ABC";
string edf = "EDF";
cout.width(8);
cout << abc;
cout.width(7);
cout << edf;
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char a[] = "ABC";
char b[] = "EDF";
cout<" "<<a<<" "<<b;
return 0;
}