Why my written codes are not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

8th Jan 2018, 2:16 PM
DEEPENDU PANDIT
DEEPENDU PANDIT - avatar
4 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; }
8th Jan 2018, 3:55 PM
Matej NovotnĂ˝
Matej NovotnĂ˝ - avatar
+ 6
Turbo C++ is obsolete, is, and should not longer be used. https://www.sololearn.com/Discuss/288609/?ref=app
8th Jan 2018, 2:52 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
@gordie thanks, I was copying it from editor on the phone so there is a possibility that something is missing 🙂
8th Jan 2018, 4:07 PM
Matej NovotnĂ˝
Matej NovotnĂ˝ - avatar
+ 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/
8th Jan 2018, 7:32 PM
Matej NovotnĂ˝
Matej NovotnĂ˝ - avatar