10 Replies - 1076 Views - Last Post: 18 June 2011 - 07:49 AM Rate Topic: -----

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

fstream and strcpy_s errors

Posted 18 June 2011 - 02:51 AM

I intended to write a code that would take the name and age of a person as input and save it to a file named Register.txt.
Here is the code:

#include <conio.h>
#include <iostream>
#include <fstream>
using namespace std;
void drawLine();
int main()
{
	char name[32]="";
	int age=0;
	ofstream outFile;
	outFile.open("Register.txt");
	cout << "Please Enter Your Name: ";
	cin >> name;
	strcpy_s(name, name);
	drawLine();
	cout << "Your name is: " << name;
	outFile << "Your name is: " << name;
	outFile << "\n";
	drawLine();
	cout << "Please Enter Your Age: ";
	cin >> age;
	cout << "\n";
	cout << "Your age is: " << age;
	outFile << "Your age is: " << age;
	drawLine();
	cout << "Thanks ! Your Name and Age has been stored in Register.txt Text File";
	_getch();
}
void drawLine()
{
	 cout << "\n";
	 cout << "================================================";
	 cout << "\n";
	 _getch();

 }


The problem is when I enter a single string for name say "John", the output is as intended,
but when I enter a space separated string, say "John Smith" the program only displays John as name and doesn't take any input for age and returns the null value. Here's the screenshot:

Posted Image

I am an absolute beginner so please explain my mistakes in syntax usage, structure etc.

Thanks :bigsmile:

Is This A Good Question/Topic? 1
  • +

Replies To: fstream and strcpy_s errors

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 02:58 AM

The >> operator is delimited by whitespace. You can get around the issue by using getline instead, which consumes everything up to (and including) the "new line" character(s).

Also, I strongly recommend you use strings rather than char arrays (char arrays come along with a lot of problems - strings are much easier to use).

I created a post a while ago which talks about the >> problem, and about string basics in C++
http://www.dreaminco...2209-c-strings/


Also, you need to be careful again mixing >> and getline in the same program, Since the >> will not consume trailing new-line characters after it has read some data.

if you try to read something using getline after having read something else using >>, then you'll probably find that the newline characters from the previous input are consumed, but the data which follows the newline is left untouched - this will almost certainly cause you problems.

Whenever you use cin >> its a good idea to also discard remaining characters afterwards. There's a similar thread which discusses that here, in the context of "holding the execution window open" (specifically, the cin.ignore function to discard characters from cin)
http://www.dreaminco...post__p__243045

This post has been edited by Bench: 18 June 2011 - 03:12 AM

Was This Post Helpful? 3
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 03:16 AM

Thanks a lot Bench ! Problem Solved ! Not to mention your article is awesome :clap:

Here's my complete and functional code for reference:

#include <conio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void drawLine();
int main()
{
	string name;
	int age=0;
	ofstream outFile;
	outFile.open("Register.txt");
	cout << "Please Enter Your Name: ";
	getline( cin, name );
	drawLine();
	cout << "Your name is: " << name;
	outFile << "Your name is: " << name;
	outFile << "\n";
	drawLine();
	cout << "Please Enter Your Age: ";
	cin >> age;
	cout << "\n";
	cout << "Your age is: " << age;
	outFile << "Your age is: " << age;
	drawLine();
	cout << "Thanks ! Your Name and Age has been stored in Register.txt Text File";
	_getch();
}
void drawLine()
{
	 cout << "\n";
	 cout << "================================================";
	 cout << "\n";
	 _getch();

 }

This post has been edited by Jeet.in: 18 June 2011 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 03:45 AM

Please stop using conio.h, especially considering you're only using getch(). It's non standard, old and not very secure. Try this instead:
cin.clear();
cin.ignore();
cin.get();
getch() might seem to work for now, but I guarantee it'll bite you in the arse at some point in the future.

Edit: By the way, why were you even using strcpy() in the first place? C++ has a string class that you can use = with:
#include <string>
#include <iostream>

int main ()
{
    std::string a = "Hello", b = "World", c;
    c = a + " " + b;
    std::cout << c << '\n';
    std::cin.get();
    return 0;
}

This post has been edited by PlasticineGuy: 18 June 2011 - 04:06 AM

Was This Post Helpful? 3
  • +
  • -

#5 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 04:32 AM

View PostPlasticineGuy, on 18 June 2011 - 03:45 AM, said:

Please stop using conio.h, especially considering you're only using getch(). It's non standard, old and not very secure. Try this instead:
cin.clear();
cin.ignore();
cin.get();
getch() might seem to work for now, but I guarantee it'll bite you in the arse at some point in the future.

Edit: By the way, why were you even using strcpy() in the first place? C++ has a string class that you can use = with:
#include <string>
#include <iostream>

int main ()
{
    std::string a = "Hello", b = "World", c;
    c = a + " " + b;
    std::cout << c << '\n';
    std::cin.get();
    return 0;
}


Thanks. To be truthful, I don't really know what
_getch()
does, I only use it to pause the program so that the output may be visible, and thanks
cin.clear();
cin.ignore();
cin.get();


also seems to bring the same effect.
Was This Post Helpful? 0
  • +
  • -

#6 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 04:37 AM

_getch() is evil, that's all you need to know!
Was This Post Helpful? 1
  • +
  • -

#7 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 04:41 AM

Quote

_getch() is evil, that's all you need to know!


Got it :D It is non standard so I need to avoid it ! And also it's platform specific !

Thanks a lot for the help though. Just thought is it necessary to learn the strcpy standards or the string syntaxes will do? I am just a beginner so your suggestion would be invaluable to me :^:
Was This Post Helpful? 1
  • +
  • -

#8 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 04:47 AM

strcpy() is only useful if you're programming in C. Otherwise C++ strings are safer and all around easier to use with minimal overhead.
Was This Post Helpful? 1
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 06:14 AM

@PlasticineGuy -- Why would _getch() be *evil*?

Because it is *non-standard* or "platforms dependent"-- to that I say, windows.h is non-standard and platform dependent, so its evil too?

conio is just a library like anything else. Its not evil. It does happen to be considered "depreciated" but every windows compiler I have ever run across (even MinGW) has it. (although most of the compilers don't use the preceding underscore).

So what is evil about it? Enlighten me.

its not like gets() which is a security risk. -- that is "standard evil" since it it part of the standard. -- so just being part of the standard does not make something "safe".

And while I am at it: void main() is not evil either. I agree that it should be avoided because it hides something from the programmer and is "non-standard" but it will not blow up the OS or fry hard drives and cause war in the middle-east. What it WILL do is get you pounced on by know-it-all forum posters.

You know I actually get mad when someone posts a question and all they get back is: Don't use void main() -- WTF. Honestly the only real problem with using void main() is that it gets you harassed on forums. Other than that the compiler slips in the returned int for you and all is well. It does not cause children to starve or rain not to fall. I suppose it *could* cause problems if you called main() recursively but that is entirely different subject.

You are correct that conio has been depreciated and should not be used. But its not evil.
Was This Post Helpful? 2
  • +
  • -

#10 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 06:15 AM

I like your analogy. I'll keep it in mind for future. Thanks Nick.
Was This Post Helpful? 0
  • +
  • -

#11 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: fstream and strcpy_s errors

Posted 18 June 2011 - 07:49 AM

Quote

And while I am at it: void main() is not evil either. I agree that it should be avoided because it hides something from the programmer and is "non-standard" but it will not blow up the OS or fry hard drives and cause war in the middle-east. What it WILL do is get you pounced on by know-it-all forum posters.

You are most correct. Even one user has got a shoot void main :gun_bandana: avatar !!!

Really like your point of view.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1