-
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");