Hi everyone,

I have got this code`

//----------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
start:
    ofstream examplefile ("example.txt");
    examplefile<<"";
    const int SIZE = 100;
    char msg[SIZE];
    
    cin.getline(msg,SIZE);
    
    examplefile << msg;
    examplefile.close();
    
    cin.get();
    system("cls");
    goto start;
    
    return 0;
}
//--------------------------------------------------------

And when I am compiling it the everything is allright at first time:it's asking you to enter some string and saving it to example.txt file
BUT, when I am writing the string again, it's not writing the first letter of that string into example.txt

I don't know what is the problem with it?


*for DMITRI KALININ*
Privet Dima, esli budit vremya pasmatri chto zdes ne vparyadke....
Vsye viglyadit i rabotait pravilno, NO v pervi raz....


Thanks for you help....

----------------------------------
Hunan Rostomyan
NJMedia Design Studio,
NJBIology Central.

First of all, don't use goto. Second of all, you are overwriting the file each time you do it.

First of all, don't use goto. Second of all, you are overwriting the file each time you do it.

But I don't think so that the problem is in goto.And also I know that I am overwriting the file ecahc time I am executing it, that's what I want to do,but I don't know the problem why only first time it's giving me results that I was waiting for, but second, 3th,....th times it's canceling the first letter?

Any ways, thank you friend fro your cooperation.

>But I don't think so that the problem is in goto.
No, but goto doesn't buy you anything where a loop is better suited.

>BUT, when I am writing the string again, it's not writing the first letter of that string into example.txt
Add some prompts to your program. Your call to cin.get() is eating the first character of your next input:

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  for ( ; ; ) {
    ofstream examplefile ("example.txt");
    const int SIZE = 100;
    char msg[SIZE];

    cout<<"Enter a line: ";
    if ( !cin.getline(msg,SIZE) )
      break;

    examplefile << msg;
    examplefile.close();

    cout<<"Press Return to continue.";
    cin.get();
    system("cls");
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.