Need major help

This is a discussion on Need major help within the C++ Programming forums, part of the General Programming Boards category; Okay, this is for a school assignment that nobody can figure out. The assignment is to get the user to ...

  1. #1
    Unregistered
    Guest

    Need major help

    Okay, this is for a school assignment that nobody can figure out. The assignment is to get the user to input something like John D. Smith. The computer then takes that and converts it into Smith, John D. The code I've tried is here. Any suggestions would be most welcome.

    #include<iostream.h>
    #include<fstream.h>

    main()
    {
    int lastnamechar=0;
    int countspace=0;
    int adder=0;
    int countspaces=0;
    int countchars=0;
    char user_name[25];
    char ch;
    ofstream outfile;
    ifstream infile;
    outfile.open("NEW.TXT",ios:ut);
    if(!outfile)
    {
    cout << "Error opening file.\n";
    return 0;
    }
    cout << "Enter your full name.";
    cin.get(user_name, 25);
    cout << '\n';
    outfile << user_name << endl;
    infile.open("NEW.TXT",ios::in);
    if(!infile)
    {
    cout << "Error opening file.\n";
    return 0;
    }
    infile.unsetf(ios::skipws);
    while(!infile.eof())
    {
    if(ch==32)
    {
    countspace++;
    }
    if(countspace>=2)
    {
    lastnamechar++;
    }
    infile >> ch;
    countchars++;
    if(countchars==1)
    {
    if((ch>96)&&(ch<123))
    {
    ch=ch-32;
    adder++;
    }
    outfile << ch;
    }
    if(countspaces>0)
    {
    if((ch>96)&&(ch<123))
    {
    ch=ch-32;
    adder++;
    }
    outfile << ch;
    }
    if(countspaces>0)
    {
    countspaces=0;
    }
    if(ch==32)
    {
    countspaces++;
    }
    if(adder==0)
    {
    outfile << ch;
    }
    if(adder>0)
    {
    adder=0;
    }
    }
    countspaces=0;
    outfile.close();
    infile.close();
    infile.open("NEW.TXT",ios::in);
    while(!infile.eof())
    {
    infile >> ch;
    if(ch==32)
    {
    countspaces++;
    }
    if(countspaces>=countspace*2)
    {
    while(adder<lastnamechar)
    {
    if(ch==0)
    { ch=44; }
    adder++;
    infile >> ch;
    cout << ch;
    if(adder==lastnamechar)
    {
    cout << ", ";
    }
    }
    }
    }
    infile.close();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Code:
    char lastname[20];
    char firstname[10];
    char midname[10];
    cout << "Enter full name";
    cin << firstname << midname << lastname;
    cout >> lastname >> ", " >> firstname >> ' ' >> midname;
    That doesn't do your file stuff, but will do input and output. You could also error check for no middle name. You could use char pointers instead, but I stuck with the arrays you had.

  3. #3
    Unregistered
    Guest
    That isn't something we were allowed to do. We have to have the _whole_ name in a single string. That's why I haven't been able to figure it out.

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you're allowed you could use a stringstream -

    Code:
    #include <iostream> 
    #include <sstream> 
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
    
    	string fullname = "John D. Smith";
    	string firstname,midname,lastname;
    	
    	istringstream iss(fullname);
    
    	iss >> firstname>>midname >>lastname ;
    
    	cout << lastname << ", " << firstname << " " << midname;
    
    	return 0;
    
    }
    or you could use something like strtok(), or if you're not allowed any functions then you could use pointers to point at the begining
    and end of each part of the name (as soon as you hit whitespace you've hit the end of the part of the name).
    zen

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    My 2c

    Just to give you my opinion, I haven't had a deatiled look at all of them, but the first source code looks like the one to use. It's simple and i think it's almost guaranteed to work.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    input the full name using getline() if you are using c_style strings and >> if you are using a string class; as per your requirement

    parse the string variable into three separate substrings (tokens) using space as the delimiter and the strok() function or write your own parsing funciton passing a single char at a time into the substring until you find a space. If space is found add the null terminating char to the substring and move on to the next substring. Once you have broken the original string into three substrings rearrange them as desired.

    If you are using a string class, it often will have a parsing function available to use.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21