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

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




easy question about strings

 
Reply to this topicStart new topic

easy question about strings, displaying a string with a minor change added to it

psydoll
1 Mar, 2007 - 08:32 AM
Post #1

New D.I.C Head
*

Joined: 22 Feb, 2007
Posts: 8


My Contributions
basically I need to have the user input their first name and then output it with asterisks in between each character eg.("Joseph" to "J*o*s*e*p*h") . Im having trouble with my loop stopping when I want it to, and I think I may need an If statement to stop putting an asterisk after the final character. Here is my code:
//this complies but gives me a bunch of jargon after the text and prints an asterisk after the final letter

const int SIZE =15;//I dont know how to get away from using this const value because i need to use a variable so the loop will work, im not sure what to do about this

char name[SIZE];

int i;//loop counter

cout<<"please enter your first name;

cin.getline(name,SIZE); //don't think i really need to use the getline but im just using it for practice

for(i=0;i<SIZE;++i) //i<SIZE isn't giving the desired effect because i debugged this and the const value doesn't change

cout<<name<<"*";

//i think this is where i need a loop
//if anyone can tell me how to fix my code i would appreciate it, i know its something silly that i am not thinking of but i cant find it

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Easy Question About Strings
1 Mar, 2007 - 08:36 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
It does not change because it is a constant. you will need to measure the length of the string itself, not the space allocated for it.Something like the following may do:
CODE

for(i=0;i<strlen(name);++i) {
   cout<<name[i];
   if(i!=strlen(name)-1)
      cout<<"*";
}


http://www.cppreference.com/stdstring/strlen.html
User is offlineProfile CardPM
+Quote Post

psydoll
RE: Easy Question About Strings
2 Mar, 2007 - 02:09 AM
Post #3

New D.I.C Head
*

Joined: 22 Feb, 2007
Posts: 8


My Contributions
QUOTE(Amadeus @ 1 Mar, 2007 - 09:36 AM) *

It does not change because it is a constant. you will need to measure the length of the string itself, not the space allocated for it.Something like the following may do:
CODE

for(i=0;i<strlen(name);++i) {
   cout<<name[i];
   if(i!=strlen(name)-1)
      cout<<"*";
}


http://www.cppreference.com/stdstring/strlen.html


oh yes that is what i needed, i hadn't learned the strlen tag in the course I am taking yet, I wonder though is there any way to do this with out using the strlen? Thank you for the response.
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Easy Question About Strings
2 Mar, 2007 - 03:12 AM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
QUOTE(psydoll @ 2 Mar, 2007 - 03:39 PM) *

I wonder though is there any way to do this with out using the strlen?


Well if you don't want to use strlen then you can have a for loop like this. :

CODE

for(i=0; name[i] != '\0' && name[i] != '\n'; i++) // remove condition for '\n' if you want

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:16PM

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