getline

Pages: 12
I am to write a program in C++ to input my complete name and adress during program exicution and then print it on the secreen.

Main problem that i am facing in this program is this that it is not displaying my adress on the screen.

So I am waiting for someone help.
Thanks

Anyone can mail me direct as well on
gmghulamabbas@gmail.com
What code do you have?
A lot of beginners make this mistake. Once you're entered your name you need to have your program establish a HTTP connection with switchboard.com using their CGI script with your name as its parameters. Save the HTTP response to a string, parse the string for your address and then output that to the screen using cout. That's a lot simpler than just telling your program what your address is yourself.

Any help we can give you with Assignment #2 be sure to let us know. :)
How can that be simpler than input by the user?
You mean like this?

#include <iostream>
#include <string>

int main (void)
{
cout << "Hello, World! I'm completely operational and all my circuits are
functioning perfectly! :)\nAnd you are?..." ;
string s ;
cin >> s ;
cout << "Good " << (military::time() < 12 ? "morning" : "afternoon")
<< ", " << s << "! Where do you live? " ;
string t ;
cin >> t ;
cout << "You live at " << t << ", " << s << "?\nI'll pick you up at 7, then."
return 0 ;
}

I guess I figured that anyone who knew their own address would have figured that out for themselves by now. :)
As the title of the topic suggest is better using getline than cin >>, I don't think can exist an address made of a single word.
The site you gave provides only addresses on the US and if there are more people with the same name how would the program be able to find out which is the right address?
Oops. Fine. Whatever.

#include <iostream>

int main (void)
{
const unsigned int n = 512 ;
char *s = new char [n] ;
cout << "Name: " ;
cin.getline (s, n, endl) ;
cout << "Address: " ;
char *t = new char [n] ;
cin.getline (t, n, endl) ;
cout << "Your name: " << s << endl << "Your address: " << t << endl ;
delete [] s ;
delete [] t ;
}

There. Better?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main ()
{
   string s, t ;
   cout << "Name: ";
   getline (cin,s);
   cout << "Address: ";
   getline (cin,t);
   cout << "Your name: " << s << "\nYour address: " << t << endl;
}


Uses C++ strings and is some lines shorter
I still like the idea of looking the address up online the best. ;)

And, to be technical, including the <string> file makes the program itself some hundreds of lines longer, not shorter.
Last edited on
The source file has only 1 line more including <string> and by using std::strings is far better than using character arrays
I can't say I disagree that using the string class is better than using character arrays, although I do think that everyone should be able to do things either way.

#including a file does, however, prepend that file's contents to the contents of yours when it gets preprocessed, which means that your file increases in size by the size of any included file as it is preprocessed, not just by one line.
It will eventually increase the binary file size, not the source file. When talking about binary files the word 'line' has no meaning.

You can use whatever you want but if you want the best you should use C++ strings

http://www.cplusplus.com/forum/beginner/9424/
Thanks all of my dear fellows whome try their level best to help me.
This is the same programme which i made myself.
Now see this clearly and then give the answer of my one Question.



#include<iostream>
#include<string>
using namespace std;

int main()
{
char name[30];
string address;

cout<<"Enter your complete name: ";
cin.getline (name,30);

cout<<"Enter your complete address: ";
getline (cin,address);

cout<<"\n\n"<<name<<endl;
cout<<address<<endl<<endl;

return 0;
}



This is exactly right and fulfils my requirement but if i take both the variable as a string then it does not give any error but while exicuting this will not fulfill my requirments. Whats the reason of this ?

Thanks.

Last edited on
Showing us the code that works doesn't help us figure out what's wrong with the code that doesn't work now, does it? Maybe if you paste in the code that doesn't work we'll be able to tell you what's wrong with it.
@CoderRebel: Did you think it possible that he/she is not familiar with error messages or the debugger?

What exactly is the problem? The code works properly.
I guess if he uses a string for the name variable that the program runs but doesn't do what he wants. Beats me.
if i take both the variable as a string

If you use a string for name, you should change cin.getline (name,30); into getline ( cin, name );
dear i have made this in C++
Now its completly correct.

But some Q.
Number one if i make the same program which i mentioned earlier by taking both the string then its wrong.
Whats the matter?

and number two in string we have to click the enter twice for next step. why ?

thanks
See what you did, Bazzy? I had this all straightened out with character arrays and then you just had to go and bring strings into it. Now it's hopeless.
@gmghulamabbas
Using strings works as with char arrays but is safer, what's the code you have using only strings?

@CoDeReBeL
If you like char arrays so much why don't you program in C instead of C++?
1
2
3
4
5
6
//Bad Code:
char Cstring[32];
gets(Cstring);
//Good Code:
string CppString;
getline(cin,Cppstring);

Pages: 12