Urgent....

really really simple program, but gets and puts not working,........can somebody spot the bug?
<\code>
#include<iostream>
#include<fstream>
#include"stdio.h"

using namespace std;

//Global Variables
char choice,test[20];

//Auxilliary Functions
void Enc()
{
char name[50];


system("cls");
cout<<"\t\t\t\tEncryptonator...\n\n";
cout<<"Input the file name(with extension) to encrypt...\n";
gets(name);
puts(name);


system("pause");
}

void Dec()
{
system("cls");
cout<<"\t\t\t\tDecryptonator...\n\n";
}

//Main Function
int main()
{
cout<<"Hello World"<<endl;
cout<<"\t\tWelcome to the Encryptonator-Decryptonator..."<<endl<<endl;
cout<<"What do you want to do?..."<<endl;
cout<<"1. Encrypt...(1)\n2. Decrypt...(2)\n3. Exit...(3)\n";
cin>>choice;


while(choice!='3')
{
system("cls");
switch(choice)
{
case '1': Enc();
break;
case '2': Dec();
break;
default: cout<<"Illegal Response...";
break;
}
cout<<"Try Again?...\n1. Encrypt...(1)\n2. Decrypt...(2)\n3. Exit...(3)\n";
cin>>choice;
}
}
<\code>

any help appreciated
Last edited on
You declared 'name' to be a char array, not an std::ifstream, and it isn't initialized. You need to pass a file name to an std::ifstream constructor before your call to the "gets()" function, and have that stream as your argument to both "gets()" and "puts()".
Last edited on
but i haven't even touched the file part, all i want is get console input into file..
My bad I mixed up the gets and puts functions. I thought they were like "get()" and "put()" where they are member functions of stream objects but they're not so ignore my last post.

EDIT: REMOVED
Last edited on
anyway, why isn't the gets executing?

and thanks for replying....
You seem to be running into the trap that most beginners hit, the "<<" operator leaves white space in the stream. So when you get to the point where you're calling "gets()" it sees the newline character and immediately returns.

I'll put my EDIT down here so that I know you see it.
but i try gets() in main it works correctly, here it doesn't even consider the statement, just skips to the next one....
Try flushing the stream cin.sync(); before you call "gets()".

EDIT: When I say "try" it's not because I'm guessing, I want you to actually write it and see what happens. The "sync()" function will essentially eat the newline character so that "gets()" doesn't see it.
Last edited on
what???
Thank a ton Cumputergeek01.........that worked............

but still didnt understan why the previous code wasn't, from where was the newline character coming?
This:
1
2
3
4
5
6
7
8
9
10
11
12
13
void Enc()
{
   char name[50];

   system("cls");
   cout<<"\t\t\t\tEncryptonator...\n\n";
   cout<<"Input the file name(with extension) to encrypt...\n";
   cin.sync(); /*RIGHT HERE!!!!*/
   gets(name);
   puts(name);

   system("pause");
}
Last edited on
i did that, and it worked..................but why?
See this post I made here: http://www.cplusplus.com/forum/general/110776/#msg604803

The sync() member function of the std::istream object makes sure there are no left over characters in the stream. See documentation here: http://www.cplusplus.com/reference/istream/istream/sync/

EDIT: The new line character was put in when you hit Enter after making your choice. This doesn't just disappear, it sits in the stream and waits for something to remove it which neither the "<<" operator or the "gets()" function do.
Last edited on
Thanks a lot Computergeek01, really appreciate the help..........
Ellipses make me nervous. Do you need me to clear up anything more?
Topic archived. No new replies allowed.