Hey guys--
I have been lurking on here for some time as a C++ student. I have a project to complete using cstring and strcpy() as a function within a "simple" program. The psedocode goes as follows:
--program accepts 2 string array inputs(using getline) 40 total chars for str1 and 20 for str2(added 1 to take in consideration the null char).
--the second array or single character(whichever it may be) has to be removed from the first array and move the null char forward.
--i understand that i have to copy each char into the first array to a location +1 from the current indexed location i am just lost on how to do it.
--it is just whenever i use strcpy(str1, str2);--the second array just replaces the whole first array. how can i replace this char individually even when i put in a string of numbers into str2?
--I am also having problems with the loop. when i prompt the question to try again and enter y/Y, it starts over as expected but puts the 2 cout prompts back to back and only accepts prompt for str2.
--i will post code to let you all see. i am a complete newb to programming. i searched the threads first. just looking for a little hand to get a hint on how to instate this couple of problems. thanks for any help! not asking for anyone to do my homework. just getting understanding before a final comes. here is the code:

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

int main()
    char str1[41];
    char str2[21];
    char ans;


    cout<<"Please enter a string (40 characters max)-->";
    cin.getline(str1, 41);

    cout<<"Please enter the characters to be removed(20 characters max) -->";
    cin.getline(str2, 21);

    strcpy(str1, str2);
    cout<<"First string with characters removed-->"<<str1<<endl;

    cout<<"Would you like to do another(y or n)?";
}while (ans!='n'||ans!='N');

return 0;

Edited by pyTony: fixed formatting

8 Years
Discussion Span
Last Post by usaxray25

You've been lurking, and that was the best title you could come up with? :icon_rolleyes:

Plus you missed all the hints to use code tags, and if you had been lurking, all the comments telling people to use code tags, and obviously, nicely formatted code as a result.
Yours however, is a mess.

And who is teaching you to use strcpy() in a C++ program anyway?

> strcpy(str1, str2);
Consider this an equivalent thing. strcpy( &str1[0], &str2[0] ); If you replace the constant subscripts with variables, can you solve it?


Well actually I tried to change that title, but it didn't edit like I wanted I guess. So don't hold back any criticism Salem, it's only my first post! No wonder some people are hesitant about asking for help here! Thank you for the input but, my God, chill out! Yes--I come to this site alot and look for answers by searching, and have been for about a year. Thank you for the hint--I will try that in my "mess" of code. I do not know all of the rules for the board yet by heart, but will work on that. Try not to flame people on their first query, at least. I am not some kid. I am an adult already in the working world trying to grasp concepts of C++. And the person who is teaching me (through distant learning mind you) is my professor at my school. So basically I am having to teach this to myself and figure it out. Thank you for your helpful comment in the midst of the blunt, arrogant other comments. I am not a jerk, I am humbly coming to you all asking for help/ hints to solve this issue. Thank you and regards.

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.