I am a beginning ISE student taking a C++ programming class and am having trouble with the simplest things. I need to write a program that reads in an address in a single line of text then outputs that address in the appropriate form.
input:
john doe # 1234 main st. # los angeles # california # 90129

ouput:
John Doe
1234 Main St.
Los Angeles, California 90129

thanks for any help you can give

Recommended Answers

All 13 Replies

Please read the announcements at the top of this forum. You will most likely get help when you show you've tried to solve your assignment, then ask questions about specific problems you're having.

Here's starting tip for you - use getline( ) specifying '#' as the delimiter, except when reading in the zipcode.

Val

yea sorry I kinda read through everything after i put it up, I will put more specific questions up sorry to everyone.

in response to the getline hint given above. I was reading about it and started writing code for it but I wanted to check and see if I had the idea right. Do I declare a few different arrays such as

char name[20];
char street[20];
char city[20];
char state[20];
int zip;

getline (name, street, city, state, zip);

//ask for input

cin.get (name, 20, '#');
cin.get (street, 20, '#');
cin.get (street, 20, '#');
//and so on

then prompt for all those things then when they are input they will be read seperatly or what, my book doesnt explain getline very well but i hope I'm somewhat on the right track

to read each of the values into a separate string, you will need separate getline statements.

from the console, you use getline thusly:

cin.getline( str_var, len_of_str_array, delimiter);

The delimiter defaults to newline, so it is not always used. In your case, you will use the '#' character for all but the last data items.

Val

So I've been playing with it and get to the point of how to read the information that I need out of the single line. I get I write a getline for every part (name, street...) but after the input "john doe # 1234 main st. # ... how does the program ditinguish which part is name and which part is street and how do I extract each part to use it later?

The file better set up so each field is entered in the in the same order. Otherwise you're SOL (usually). Since you know how the file is set up, you know which field comes first, which comes second, etc.

just throwing it out there and dont know if this is right but here goes nothing:

cout << "Enter you address starting with your name then street then city then state then zip code, seperate each section with a #, for example name # street # ... then after you have entered everything enter a period after the zip code to ensure you are done.";

getline (name, 20, '#');
getline (street, 20, '#');
getline (city, 20, '#');
getline (state, 20, '#');
getline (zip, 20, '.');

cout << endl;

then go on to output the info received into acceptable form

This is what i put together so far, it comes out with a lot of errors that I dont understand and it could be i just dont really understand what I need to be doing. If there are any blatent mistakes any comments are greatly appreciated even if they are "you're dumb and this is totally wrong" At least i know I'm not doing it right and I can try again. Thanks guys

#include <iostream>
#include <cstring>

using namespace std;

void address(char name, char street, char city, char state, char zip);

int main()
{
        char name[20];
        char street[20];
        char city[20];
        char state[20];
        char zip[6];

return 0;
}

void address(char name, char street, char city, char state, char zip)
{

        cout<<"Enter your address starting with your name then street then
               city then state then zip code, seperate each section with a
               #, example name # street # city # ... then after the zip
               code is entered enter a period.";

        cin.getline (name, 20, '#');
        cin.getline (street, 20, '#');
        cin.getline (city, 20, '#');
        cin.getline (state, 20, '#');
        cin.getline (zip, 6, '.');

        cout<<endl;
}

after this I will do another function that will print out the info in a proper form like the one in my first message but that shouldnt be hard at all so I didnt bother putting it down.

Your function, as written, is getting only single characters as the arguments, and since they are passed by value, nothing in the function will change them in any useful way. Your parameters need to be arrays of char, as in:

void address( char name[], char street[] ......

Val

alright so I fixed everything and tried to compile it and it worked. The only problem now is that when I try and run the program absolutely nothing comes up I would expect it to at least ask me for the address but it doesnt. I have the same as the other thing but I added [] after all the char and that made it compile but if you have a reason why nothing would come out when I ran the program please let me know

If you've not made any other changes, then the program is doing exactly what you told it to.

main( ) allocates some storage. That's all it does. You must call the address function in main( ) for anything to happen.

so the program runs and gets all the information out but now I need to be able to out put them in the right for and I'm having trouble with that. right now I have:

cout << " The address you entered is: \n";
cout << name;
cout << street;
cout << city;
cout << state;
cout << zip;

I've tried just about everything i can think of to make it appear in the right form but none of it has worked, so I put it back in the first basic form that I had it. If you could suggest a way to drop the next line of text down to the next line after name and such that would be great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.