0

hello i have a c++ code for convert latin alphabet to arabic alphabet It works well for all characters except space character When space character needs to be printed in the file, no character is printed in the file how i can fix it ?


 #include <iostream>
 #include <string>
#include <fstream> 
#include <cstdlib>
 using namespace std;

int main()
 {

// ofstream constructor opens file
ofstream outClientFile( "f2.txt", ios::out );

 // exit program if unable to create file
 if( !outClientFile )// overloaded ! operator
 {
 cerr << "File could not be opened" << endl;
 exit( 1 );
 } // end if

 cout << "Enter Your Text:" << endl;
 
outClientFile<<"your text is :"<<endl;


char ch;


cin >> ch;
 
    
      if (ch == 'a') outClientFile << "ش";
     else if (ch == 'b') outClientFile << "ذ";
     
     else if (ch == ' ') outClientFile << " ";
     else if (ch == '.') outClientFile << " ";
      else outClientFile << ch;

 } // end main


mostafa
  • 51
  • 2
  • 7
  • You should properly format and indent your code first. What editor are you using? – Dai Mar 10 '21 at 14:09
  • Does this answer your question? [How to cin Space in c++?](https://stackoverflow.com/questions/2765462/how-to-cin-space-in-c) – 273K Mar 10 '21 at 14:11

1 Answers1

2

Instead of:

cin >> ch;

you should use:

ch = getchar();

This way you'll be able to read spaces too.

Don't forget to include:

#include <cstdio> 
Damian
  • 1,084
  • 3
  • 14
  • 26
  • `std::getline` would be better. – sweenish Mar 10 '21 at 14:32
  • As documented here: http://www.cplusplus.com/reference/string/string/getline/ this function is for strings. He wants to read character by character, as per his description. – Damian Mar 10 '21 at 14:33
  • I am well aware of `std::getline`, and nowhere does it state that they want to **read** character by character. The code implies that, but they don't state it. And code implications could mean all manner of things, including that they don't know about better alternatives. It would be better to separate the acquisition of the input from the task of changing characters. And I would refrain from linking to a site that still thinks C++11 is the new stuff. – sweenish Mar 10 '21 at 14:38
  • @sweenish sure, if you have something new, link to it. I don't think that `std::getline` got any new updates since C++11, hence the link in discussion doesn't contain any outdated information. The answer I gave him was purely so he can continue his work, instead of telling him "you shouldn't do it like this, this is the worst you can do". If the question in cause was "Can I do this better?" then of course I would give him another answer, not this one. – Damian Mar 10 '21 at 14:42
  • So you'll just juggle between an outdated site and a better one? At no point did I imply it was the worst thing he could do. The code, as presented, reads one character only. Your answer, while it does get them on their way, also required a new include when it wasn't necessary. – sweenish Mar 10 '21 at 14:50
  • Sure, there are a lot of approaches that he can take to do what he desires. This is one way. If he finds it useful, he marks it as a solution that solved his problem. – Damian Mar 10 '21 at 14:54