Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,071 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,806 people online right now. Registration is fast and FREE... Join Now!




Simple String Program

 
Reply to this topicStart new topic

Simple String Program

mastershredder
2 Feb, 2008 - 10:03 AM
Post #1

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 17


My Contributions
Im writing this simple program that will add to entered strings> It goes:

1. Enter a string i.e. "I love"
2. Enter another string "New York"
3. Result displays " I love New York"

Heres the code, this is probably an easy fix and I believe all but the result routine are correct.

CODE
#include <iostream>
#include <cstring>


using namespace std;


int prompt(void);
int prompt_again(void);
int display(void);


int rc = 0;
char line[20];
char line2[20];
char line3[20];


int main()
{
    rc = prompt();
    rc = prompt_again();
    rc = display();
    return rc;
}



int prompt()
{
    cout << "Enter A String " << endl;
    cin.getline(line, 20);

    return 0;
}




int prompt_again()
{
    cout << "Enter Another String " << endl;
    cin.getline(line2, 20);

    return 0;
}



int display()
{
    line3 = line[20] + line2[20];

    cout << "The New String Is: " << line3 << endl;

    return 0;
}

User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Simple String Program
2 Feb, 2008 - 10:40 AM
Post #2

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
You can use C++ style strings. That's unless it is a homework and you have to do it a certain way.

CODE

#include <iostream>
#include <string>

using namespace std;

int prompt();
int prompt_again();
int display();

int rc = 0;

string line;
string line2;
string line3;


int main()
{
    rc = prompt();
    rc = prompt_again();
    rc = display();
    return rc;
}



int prompt()
{
    cout << "Enter A String ";
    getline(cin, line);

    return 0;
}


int prompt_again()
{
    cout << "Enter Another String ";
    getline(cin, line2);

    return 0;
}


int display()
{
    line3 = line + " " + line2;

    cout << "\nThe New String Is: " << line3 << "\n\n";

    return 0;
}


Also i don't understand why display(), prompt_again(), prompt() return a 0. I would make the functions void and eliminate some things:
CODE

#include <iostream>
#include <string>

using namespace std;

void prompt();
void prompt_again();
void display();

string line;
string line2;
string line3;


int main()
{
    prompt();
    prompt_again();
    display();

    system("PAUSE");
    return 0;
}



void prompt()
{
    cout << "Enter A String ";
    getline(cin, line);
}


void prompt_again()
{
    cout << "Enter Another String ";
    getline(cin, line2);
}


void display()
{
    line3 = line + " " + line2;

    cout << "\nThe New String Is: " << line3 << "\n\n";
}


This post has been edited by #include<wmx010>: 2 Feb, 2008 - 10:52 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Simple String Program
2 Feb, 2008 - 11:19 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
The original problem is arising because you cannot use the + operator to concatenate character arrays. In that case, you'd use the strcat() function. You can use + operator to concatenate C++ string objects, as loosely demonstrated above.

All that being said, you should avoid the use of global variables where possible (and it is certainly possible here).
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 10:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month