Thread: Char not setting characters as expected

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    4

    Char not setting characters as expected

    Hi all.

    First off, cheers to the Cprogramming.com guys, I've tryed a couple of times to get into C++ with no sucess, but after a couple of hours here, I feel I am now on the road (Or at least I am in the car and the engine is running, now I'm just trying to find the map )

    Anyway, I am trying to learn by reading some, then making simple programs as I go. Currently I am working on a program to take a CSV file from a server, copy it locally, remove unwanted lines and format the remaining lines into commands (For a batch file).

    Here is the code I have so far:

    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std; // So the program can see cout and endl
    
    int main()
    {
        
        int buildname;
        char bnamecorrect;
        char N = N;
        char Y = Y;
        int namedone = 0;
        
        
        /*Display welcome and current buildname*/
        /*Getting data from file*/
        while (namedone == 0){
        ifstream a_file ( "buildname.txt" );
        if ( !a_file.is_open() ) {
              // The file could not be opened
              cout<<"No build name found. Please enter below\n";
              cin>>buildname;
              /*Close file for reading (Just in case)*/
              a_file.close();
              /*Open file for editing*/
              ofstream a_file ( "buildname.txt" );
              /*And write info*/     
              a_file<<buildname;
              /*Close it again*/
              a_file.close();
        }
        else {
            // Safely use the file stream
            a_file.close();
            /*Open file and read build name*/
            ifstream a_file ( "buildname.txt" );
            a_file>>buildname;
            /*Output buildname and check if correct*/
            cout<<"Current buildname is "<<buildname<<", is this correct? Y/N\n";
            cin>>bnamecorrect;
            /*If buildname is not correct, get new name and add it to file*/
            cout<<bnamecorrect<<"\n"<<N<<"\n"<<Y<<"\n";
            if (bnamecorrect == N){
                 cout<<"Please enter new build name\n";
                 cin>>buildname;  
                 /*Open file for editing*/
                  ofstream a_file ( "buildname.txt" );
                 /*And write info*/  
                 a_file<<buildname;
                 namedone = 1;
            }    
            else if (bnamecorrect == Y){
                cout<<"Procceeding with buildname "<<buildname<<"\n";
                     namedone = 1;
                }    
            else {
                 cout<<"Please use Y or N\n";   
            }    
    }    
        
        }
        cout<<"Welcome to the test tool\n";
        cout<<"";
        cin>>buildname;
        cin.get();
    }
    What that should do so far is open a text document which contains the build number (Its the dynamic part of the path to the CSV file) and display it. It should ask if the buildname is correct and the user enters Y or N and the program continues accordingly. However, when I cout the N and Y vars they come up as '?' and '$' respectivley.

    Anyone explain why they are doing that?

    Thanks for your help - Linkjames

    p.s. No doubt I am going about this all wrong. If I am doing something blatently wrong, I would also appreciate someone telling me so, and if possible, pointing me in the correct direction.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std; // So the program can see cout and endl
    
    int main()
    {
        
        int buildname;
        char bnamecorrect;
        char N = 'N';  // Forgot quotes
        char Y = 'Y';  // Forgot quotes
        int namedone = 0;
        
        
        /*Display welcome and current buildname*/
        /*Getting data from file*/
        while (namedone == 0){
        ifstream a_file ( "buildname.txt" );  // You open the file
        if ( !a_file.is_open() ) {
              // The file could not be opened
              cout<<"No build name found. Please enter below\n";
              cin>>buildname;
              /*Close file for reading (Just in case)*/
              a_file.close();
              /*Open file for editing*/
              ofstream a_file  ( "buildname.txt" );  // Variable name should be different from a_file
              /*And write info*/     
              a_file<<buildname;
              /*Close it again*/
              a_file.close();
        }
        else {
            // Safely use the file stream
            a_file.close();                       // Then you close it
            /*Open file and read build name*/
            ifstream a_file ( "buildname.txt" );  // Then you create a new variable w/ same name and reopen?
                                                  // This line of code should not be needed
            a_file>>buildname;
            /*Output buildname and check if correct*/
            cout<<"Current buildname is "<<buildname<<", is this correct? Y/N\n";
            cin>>bnamecorrect;
            /*If buildname is not correct, get new name and add it to file*/
            cout<<bnamecorrect<<"\n"<<N<<"\n"<<Y<<"\n";
            if (bnamecorrect == N){
                 cout<<"Please enter new build name\n";
                 cin>>buildname;  
                 /*Open file for editing*/
                  ofstream a_file ( "buildname.txt" );
                 /*And write info*/  
                 a_file<<buildname;
                 namedone = 1;
            }    
            else if (bnamecorrect == Y){
                cout<<"Procceeding with buildname "<<buildname<<"\n";
                     namedone = 1;
                }    
            else {
                 cout<<"Please use Y or N\n";   
            }    
    }    
        
        }
        cout<<"Welcome to the test tool\n";
        cout<<"";
        cin>>buildname;
        cin.get();
    }
    Last edited by hk_mp5kpdw; 11-30-2004 at 09:44 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Perfect, thanks. Don't go poking holes in my code, you'll make me cry j/k

    Not sure why I put that in there. Anyway, all is good now, thank you very much.

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. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM