Thread: string problems

  1. #1
    Covenent Killer
    Join Date
    Jul 2005
    Posts
    26

    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?

  2. #2
    Newbie Finneous's Avatar
    Join Date
    Aug 2005
    Location
    Finland
    Posts
    7
    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.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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";
        }

  4. #4
    Covenent Killer
    Join Date
    Jul 2005
    Posts
    26
    i was wondering about that... it was strange to me why they didnt use brackets... thanks!

  5. #5
    Covenent Killer
    Join Date
    Jul 2005
    Posts
    26
    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...)

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    73
    Insert a newline character, the '\n'.

    An easier way would be to say:

    b_file << endl;

    ... just like cout

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.
    Last edited by 7stud; 08-01-2005 at 05:31 PM.

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you should use
    Code:
    #include <string>
    
    string name;
    getline(cin, name, '\n');

  9. #9
    Covenent Killer
    Join Date
    Jul 2005
    Posts
    26
    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!

  10. #10
    Covenent Killer
    Join Date
    Jul 2005
    Posts
    26
    ujst out of curiosity -- would there be a way to send this data via email? that would be cool!

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Using mailto: from the command line (on Linux). I don't know how to do it in Windows.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Covenent Killer
    Join Date
    Jul 2005
    Posts
    26
    thats the problem... im running windows

  13. #13
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    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.

  14. #14
    Covenent Killer
    Join Date
    Jul 2005
    Posts
    26
    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...
    Last edited by Halo2Master; 08-02-2005 at 05:19 PM.

  15. #15
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    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");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM