Home Forums C Programming Some problems in C++

Viewing 14 reply threads
  • Author
    Posts
    • #2175
      050449
      Participant

      I started C++ two days ago and need your help


      #include
      #include
      #include
      #include
      #include
      #include


      using namespace std;
      char sendtext;
      char username;
      void read(){
      system("cls");
      cout << "You autorised as:" << username << "n";
      ifstream in;
      in.open("msngr.txt");
      if (!in) {
      cout << "Input file cannot be opened.n";
      //return(1);
      }
      char str[20000];
      while (!in.eof()) {
      in >> str;
      cout << str << "n";
      strcpy(str, "");
      }
      cout << "n";
      in.close();

      }
      void
      write(){

      cin >> sendtext;
      if(sendtext){
      FILE * pFile;
      char yazi[] = {sendtext , 'n'};
      pFile = fopen ( "msngr.txt" , "a+" );
      fwrite (yazi , 1 , sizeof(yazi) , pFile );
      fclose (pFile);
      read();
      write();
      }
      }
      main(){
      cout << "Enter your username: ";
      cin >> username;
      read();
      write();


      return (0);
      }

      why its not work properly? Please help.

    • #3506
      GWILouisaxwzkla
      Participant

      The first function “read” looks ok to output the file data one word per line ,except I would probably pass the user’s name as a parameter instead of a global variable :


      void read( char * userName );

      the second function for adding data to a file ( not sure why you used both fstream.h and stdio.h for file input / output in the same program ( ? ) ) seems to have some problems. First I would allocate an array of the proper size and use strcpy ( string.h ) to copy the data.


      char yazi [ MAX_SIZE ];
      strcpy ( yazi , sendText );

      Next , you should take read() and write() out of the conditional or the program will never end :


      if ( sendtext )
      {
      FILE * pFile;
      char yazi[] = {sendtext , 'n'};
      pFile = fopen ( "msngr.txt" , "a+" );
      fwrite (yazi , 1 , sizeof(yazi) , pFile );
      fclose (pFile);
      read(); //take these out!
      write();
      }

      thats what I see so far…..

    • #3507
      050449
      Participant

      thanks for all but there is error

      `MAX_SIZE’ undeclared (first use this function)

    • #3508
      050449
      Participant

      thanks for all but there is error

      `MAX_SIZE’ undeclared (first use this function)

    • #3509
      050449
      Participant

      i defined MAX_SIZE and this problem solved but now it shows me

      invalid conversion from `char’ to `const char*’
      initializing argument 2 of `char* strcpy(char*, const char*)’

    • #3510
      050449
      Participant

      if ( sendtext )
      {
      FILE * pFile;
      char yazi[] = {sendtext , ‘n’};
      pFile = fopen ( “msngr.txt” , “a+” );
      fwrite (yazi , 1 , sizeof(yazi) , pFile );
      fclose (pFile);
      read(); //take these out!
      write();
      }

      if i take this out my new text will not appear

    • #3511
      GWILouisaxwzkla
      Participant

      for the write function I would do something like ( I don’t think I would have the console prompt inside the function , but if this is what you want ):



      void write ()
      {

      char choice;
      cout << "do you want to input data to file ( y or n )" << endl;
      cin >> choice;
      cin.get(); //get newline
      while ( choice != 'y' && choice != 'n' )
      {
      cout << "bad choice!" << endl;
      cout << "do you want to input data to file ( y or n )" << endl;
      cin >> choice;
      cin.get(); //get newline
      }

      if ( choice == 'y' )
      {
      char inputText [ MAX_NAME ];
      cout << "enter input text: " << endl;
      cin.getline ( inputText , MAX_NAME , 'n' );

      ofstream outputFile;
      //PUT YOUR DATA FILE NAME HERE!
      outputFile.open ( "c:\programs\help\data3.txt" , ios::app );
      outputFile << inputText << endl;
      outputFile.close();

      }

      }
    • #3512
      050449
      Participant

      thanks it works ok.
      now code is


      #include
      #include
      #include
      #include
      #define MAX_NAME 1000


      using namespace std;
      char sendtext;
      char username;

      void read(){
      system("cls");
      cout << "You autorised as:" << username << "n";
      ifstream in;
      in.open("msngr.txt");
      if (!in) {
      cout << "Input file cannot be opened.n";
      //return(1);
      }
      char str[20000];
      while (!in.eof()) {
      in >> str;
      cout << str << "n";
      strcpy(str, "");
      }
      cout << "n";
      in.close();

      }
      void write ()
      {

      char choice;
      cout << "do you want to input data to file ( y or n )" << endl;
      cin >> choice;
      cin.get(); //get newline
      while ( choice != 'y' && choice != 'n' )
      {
      cout << "bad choice!" << endl;
      cout << "do you want to input data to file ( y or n )" << endl;
      cin >> choice;
      cin.get(); //get newline
      }

      if ( choice == 'y' )
      {
      char inputText [ MAX_NAME ];
      cout << "enter input text: " << endl;
      cin.getline ( inputText , MAX_NAME , 'n' );

      ofstream outputFile;
      //PUT YOUR DATA FILE NAME HERE!
      outputFile.open ( "msngr.txt" , ios::app );
      outputFile << inputText << endl;
      outputFile.close();
      read();
      write();
      }

      }
      main(){
      cout << "Enter your username: ";
      cin >> username;
      read();
      write();


      return (0);
      }

      but there is some problems too.
      when i enter username it writes only first the letter of the username and all other letters understands as answer to the confirmation and writes

      You autorised as:N
      do you want to input data to file ( y or n )
      bad choice!
      do you want to input data to file ( y or n )
      bad choice!
      do you want to input data to file ( y or n )

      and when it shows the data of the file it puts n after all words and when i write “something is wrong”
      it shows
      something
      is
      wrong
      but in file everything is ok

    • #3513
      GWILouisaxwzkla
      Participant

      This seems to work:



      #include
      #include
      #include
      #include
      #include
      #define MAX_NAME 1000


      using namespace std;
      char sendtext;
      char username [ MAX_NAME ];

      void read(){
      system("cls");
      cout << "You autorised as:" << username << "n";
      ifstream in;
      in.open("c:\programs\in1.txt");
      if (!in) {
      cout << "Input file cannot be opened.n";
      //return 1;
      }
      char str[20000];
      while (!in.eof()) {
      in >> str;
      cout << str << "n";

      }
      cout << "n";
      in.close();

      }
      void write ()
      {
      char badInput;
      char choice;
      cout << "do you want to input data to file ( y or n )" << endl;
      cin >> choice;
      cin.get(); //get newline
      while ( tolower ( choice ) != 'y' && tolower ( choice ) != 'n' )
      {
      badInput = cin.get();
      while ( badInput != 'n' ) //clear garbage out of stream
      {
      badInput = cin.get();
      }
      cout << "bad choice!" << endl;
      cout << "do you want to input data to file ( y or n )" << endl;
      cin >> choice;
      cin.get(); //get newline
      }

      if ( tolower ( choice ) == 'y' )
      {
      char inputText [ MAX_NAME ];
      cout << "enter input text: " << endl;
      cin.getline ( inputText , MAX_NAME , 'n' );

      ofstream outputFile;
      //PUT YOUR DATA FILE NAME HERE!
      outputFile.open ( "msngr.txt" , ios::app );
      outputFile << inputText << endl;
      outputFile.close();
      read();
      write();
      }

      }
      main(){
      cout << "Enter your username: ";
      cin >> username;
      cin.get();
      read();
      write();


      return (0);
      }
    • #3514
      050449
      Participant

      Oh thanks it works well.

      but can you explain what “cin.get()” means??
      and what it used for?

      and now i have to think how to remove confirmation

    • #3515
      050449
      Participant

      ups problem. problem with read not solved it shows each word in new line

    • #3516
      GWILouisaxwzkla
      Participant

      cin.get() is a member function that gets the next character from the input stream:


      char ch;

      ch = cin.get(); //get a single character from the keyboard and store in 'ch'
      ch = cin.get(); //get a single character from the keyboard , but don't store it ( throw it out )

    • #3517
      050449
      Participant

      thanks.and what about read() function ??

    • #3518
      GWILouisaxwzkla
      Participant

      I think read() is for arrays. Check out this link on the topic: http://www.cplusplus.com/reference/iostream/istream/read.html

    • #3519
      050449
      Participant

      Hi. I have a new problem now.

      I am using C++ builder 2009 and i cannot make “tmaskedit” to show characters as “*” like in web

      Can anyone help me?

Viewing 14 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.