-
string problems
ok, i am making a database -- here is the code, i marked what the compiler is angry about...
Code:
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <fstream>
#include <windows.h>
#include <time.h>
using namespace std;
int main()
{
system("cls");
cout << " Please Enter Your Information." << endl;
cout << endl;
char string0[256];
cout << " Please Enter Your Name: ";
cin.getline ( string0, 256, '\n' );
char string[256];
cout << " Please Enter Your Address: ";
cin.getline ( string, 256, '\n' );
char string1[256];
cout << " Please Enter Your 2nd Address (Optional): ";
cin.getline ( string1, 256, '\n' );
char string2[256];
cout << " Please Enter Your City: ";
cin.getline ( string2, 256, '\n' );
char string3[3];
cout << " Please Enter Your State (XX): ";
cin.getline ( string3, 3, '\n' );
char string4[10];
cout << " Please Enter Your Zipcode: ";
cin.getline ( string4, 10, '\n' );
char string5[256];
cout << " Please Enter Your Phone Number: ";
cin.getline ( string5, 256, '\n' );
cout << endl;
cout << endl;
cout << endl;
cout << " Is This Information Correct?" << endl;
cout << endl;
cout << " Name: " << string0 << endl;
cout << " Address: " << string << endl;
cout << " Address(2): " << string1 << endl;
cout << " City: " << string2 << endl;
cout << " State: " << string3 << endl;
cout << " Zip: " << string4 << endl;
cout << " Phone: " << string5 << endl;
cout << endl;
char sure[2];
cout << " Is This Information Correct? (Y/N): ";
cin.getline ( sure, 2, '\n' );
if ( strcmp ( sure, "Y" ) == 0 )
system("cls");
cout << " Great! Your Information Has Been Recorded!" << endl;
ofstream a_file ( "information.txt", ios::app );
a_file << string0 << string << string1 << string2 << string3 << string4 << string5;
a_file.close();
Sleep(5000);
system("exit");
else
system("cls");
cout << " This Is Your Last Chance!" << endl;
char string7[256];
cout << " Please Enter Your Name: ";
cin.getline ( string7, 256, '\n' );
char string8[256];
cout << " Please Enter Your Address: ";
cin.getline ( string8, 256, '\n' );
char string9[256];
cout << " Please Enter Your 2nd Address (Optional): ";
cin.getline ( string9, 256, '\n' );
char string10[256];
cout << " Please Enter Your City: ";
cin.getline ( string10, 256, '\n' );
char string11[3];
cout << " Please Enter Your State (XX): ";
cin.getline ( string11, 3, '\n' );
char string12[10];
cout << " Please Enter Your Zipcode: ";
cin.getline ( string12, 10, '\n' );
char string13[256];
cout << " Please Enter Your Phone Number: ";
cin.getline ( string13, 256, '\n' );
system("cls");
cout << " Your Information Has Been Recorded!" << endl;
ofstream b_file ( "information.txt", ios::app );
b_file << string7 << string8 << string9 << string10 << string10 << string11 << string12 << string13;
b_file.close();
cin.get();
}
ok, the first error reads "expected primary expression before 'else'" and the second error for the same line says "expected `;' before 'else'"
the tutorial i went off of is this:
Code:
if ( strcmp ( name, "Julienne" ) == 0 )
cout<<"That's my name too.\n";
else
cout<<"That's not my name.\n";
HOW IS THAT DIFFERENT?
-
You don't have the little curly brackets..
eg.
Code:
if (conditions)
{
statements;
}
else
{
statements;
}
note that you don't need those for
Code:
if (conditions)
statement;
else
statement;
Hope it helps.
-
The turorial shows only one "if" line and one "else" line.
You need brackets to group all of the conditional stuff together.
The turtorial could have been written this way:
Code:
if ( strcmp ( name, "Julienne" ) == 0 )
{
cout<<"That's my name too.\n";
}
else
{
cout<<"That's not my name.\n";
}
-
i was wondering about that... it was strange to me why they didnt use brackets... thanks!
-
one more question, to make the database neat, how would i make the information going into the text file be in seperate lines?
Example: John Doe
2222 Weird Street
(ect...)
-
Insert a newline character, the '\n'.
An easier way would be to say:
b_file << endl;
... just like cout :)
-
I don't think you want to use:
bfile <<endl;
It forces the buffer to flush, which makes writing inefficient. When you try to write to a file, you don't actually write to the file every time. The output is put into a buffer, and the data is written to the file when the buffer is full. It's more efficient to write big chunks of data to the file occasionally than constantly write small pieces of data to the file. Since endl forces the buffer to be written to the file whether the buffer is full or not, I think the file will get written to more times than is necessary. Just write a '\n' character to the file whenever you want a newline, and let the buffer write to the file on its own.
-
you should use
Code:
#include <string>
string name;
getline(cin, name, '\n');
-
i used the this system because i needed to limit the amount of characters going into my program, i'll use that for more practical jobs -- Thanks for your help!
-
ujst out of curiosity -- would there be a way to send this data via email? that would be cool!
-
Using mailto: from the command line (on Linux). I don't know how to do it in Windows.
-
thats the problem... im running windows
-
I could swear there was an easy way to use telnet scripts in Windows, with which you could contact an SMTP daemon...but I searched for about 20 minutes and either I really suck at searchig or something like that (standard) is really hard to find.
-
i searched for like an hour and nothing... at times, linux has the answers. i could do it through FTP! I made a code that uses basic I/O but I cant get it to send through command prompt... I get it to open ftp client but then the "system("command")" doesnt work... if someone can find a way to do so, that would be more secure than sending over internet, because then someone would have to get into mt FTP to get information, plus the user cant change the info once sent...
-
If using FTP is ok, there is a way to run FTP scripts with Windows - Make a file with the extension ".ftp" with one FTP command per line, and call it with something like:
system("ftp -s:commandlist.ftp");
-
ok... i never thought about that! im new to FTP so im learning a little at a time... I found ways to do FTP in VC++ and C#, but couldn't find anything for C++ on MSDN
-
while were talking about FTP, if you connect through FTP to astecinc.95mb.com and put in username astecinc.95mb.com, it brings up the password input... when you type stuff, the cursor never moves -- I dont want to do that but I do want to mask the text with asteriks... I can do this in HTML and in JavaScript, but can someone give me pointers? I'm researching right now and if i find something, I'll post for you guys...
-
nazca -- thanks soooo much, but actually -- you don't need the extension to be .ftp, it needs to be .txt, but thanks for the advise! now back to character masking...
I made a code using that principle, but i need help on how to get rid of the file -- see the code and i marked the files in red that need to be deleted before the program ends... I know that you use the "ios::trunc" function, but how do i use it?
Code:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <windows.h>
#include <fstream>
using namespace std;
int main()
{
system("title Demo Project For cprogramming.com!");
string info;
cout << " Please Enter Your Name: ";
cin >> info;
ofstream a_file ("info_name.txt", ios::app);
a_file << info;
a_file << endl;
a_file.close();
ofstream b_file ("ftpsend.txt");
b_file << "o" << endl;
b_file << "your.FTP.com" << endl;
b_file << "your.Username" << endl;
b_file << "your.Password" << endl;
b_file << "send info_name.txt" << endl;
b_file << "bye" << endl;
b_file.close();
system("ftp -s:ftpsend.txt");
system("cls");
cout << " Press [Enter] To Exit This Program!" << endl;
cin.get();
}
Do I need to classify another ofstream x_file?
-
I'm sure there's a much, much less hacky way to do it but as far as what you're asking goes:
Code:
a_file.close();
b_file.close();
ofstream clearThis("info_name.txt", ios::trunc);
ofstream thisToo("ftpsend.txt", ios::trunc);
clearThis << endl; // not sure if it's necessary but figure it'll make sure everything's cleared
thisToo << endl;
clearThis.close();
thisToo.close();
If you ask me, while you're using system calls you might as well use
Code:
system("del info_name.txt");
system("del ftpsend.txt");
Which will actually destroy the files instead of emptying them. There's probably an ANSI standard way to do it, too.
Btw - on my box, files with the ".ftp" extension will work, but those with ".txt" will not. Strange. Maybe I mixed it up?
-
Masking Issues
hmmm... good point. I tried using the first method before i posted the question but it didnt do anything...
Now back to the masking issue... I found a way to do it through VC++ and VB, and i did find one way to do it through C++ but i cant get it to work anymore... i talked with another guy who said to use a combo of "\b" which is backspace and a find x,y function... basically telling the computer to backspace and replace the character with an asterisk (*) but hold the data using virtual memory, or something like that.
Can someone clear this up for me?