+ 3
Why my written codes are not working?
i am learned c++ in my school and there all the codes are working fine but here it is not working at all . In my school i m working on turbo c++. i write down all the programmes that are working out there but..here they are working. why?¿¿? đ˘đđđ U can see my codes https://code.sololearn.com/cGHy77t5hdo0/?ref=app https://code.sololearn.com/c2CX9EjenwII/?ref=app
5 Answers
+ 2
Your code modified to work as should.
To store the input use string instead of char, what if the input will be longer than 80? To read to string use getline (it will read whole input including spaces until newline character is recognized).
In for loop you need to define i < string.length().
If you want to replace space with asterisk you have to use single quotes â*â.
Hope itâs all correct, here itâs working đ
#include <iostream>
#include <string>
using namespace std;
int main() {
string n;
int i ,c=0 ;
cout<<"enter a string\n";
getline(cin,n);
for(i=0;i<n.length();i++)
{
if (n[i]==' ')
n[i]= '*';
}
cout<<n;
return 0;
}
+ 6
Turbo C++ is obsolete, is, and should not longer be used.
https://www.sololearn.com/Discuss/288609/?ref=app
+ 1
@gordie thanks, I was copying it from editor on the phone so there is a possibility that something is missing đ
+ 1
At the beginning of the file youâve defined #include<string> meaning that you will have access to whole string class and itâs functions since âstringâ is class. One of the functions this class contains is âlengthâ which returns integer value equal to the length of the string (number of characters in string). If you want to access the function of class, you need to use dot notation: nameOfObject.function(), in your example n.length().
As for i<n.length(): itâs the condition of the for loop meaning that the for loop will repeat until above specified condition will be valid (while âiâ is smaller in value than the length of string n).
For more info about string class check below page:
http://www.cplusplus.com/reference/string/string/