Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




acromatic string

 
Reply to this topicStart new topic

acromatic string

tis4pgms
28 Sep, 2008 - 02:14 AM
Post #1

New D.I.C Head
*

Joined: 14 Sep, 2008
Posts: 9


My Contributions
quoted by:tis4pgms
CODE

#include<stdio.h>
main()
{
char ch;
int i;
printf("entr  the string");
gets(ch);
while(ch[i]!='\0')
{
if (ch[i]==' '0
{
i++;
printf("%c",ch[i]);
}
i++;
}
}



this is the code i entered 2 get the acromatic of the string.but as i enter into the o/p screen after successful compilation i can't enter a string of size more than 4. help me plz......................plz.........plz...........................blink.gif

User is offlineProfile CardPM
+Quote Post

Psionics
RE: Acromatic String
28 Sep, 2008 - 05:36 AM
Post #2

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 122



Thanked: 2 times
Dream Kudos: 100
My Contributions
To be completely honest, I'm not too sure why you're making it like this, but what you're doing isn't legal. You made a char and an int, and then somehow turned that into an array and tried to access the index of the array checking for the null terminator. Since you're not including the String class, I'm going to assume you don't know how to use that, so I'll offer you some guidance until someone else posts. I learned to make strings using character arrays. It'll work basically the same:

cpp

// get access to cin and cout
#include <iostream>
using namespace std;

int main( void )
{
// initialize a char array of 100
// or whatever size
char arr[ 100 ] = { };

// ask the user to input
cout << "Enter String: ";

// use cin.get() to grab the string
// cin.get( variableName, size );
cin.get( arr, 100 );


cin.get() offers us some useful techniques, such as skipping over spaces if the user types two words. Now if you're error checking to make sure the user input something, I like to use a simple do{}while() loop to check:
cpp

do
{
// ask for input
cout << "Enter String: ";
cin.get( arr, 100 );

// check if user input SOMETHING
// makes sure user didn't just hit enter
if( cin.good() )

// if something entered
break;

// now we clear the failbit
cin.clear();

// get rid of the bad data
cin.ignore( INT_MAX, '\n' );

} while( true );

// it's good practice to use the ignore again
// after the loop
cin.ignore( INT_MAX, '\n' );


If you don't use the cin.ignore() call, the next time you try to use cin in any way, it won't work!! I just used INT_MAX to ignore the maximum number of values the user could have entered, and then the newline to ignore that also.

Again, I'm not really sure what you're trying to do, so if this doesn't answer your question, please let us know more specifically what troubles you're having.


****EDIT****
I did a little google search ( something you should have done smile.gif ) and found this link for you. The example uses pointers (which is how you should do this) so be sure to post if you don't understand pointers!

This post has been edited by Psionics: 28 Sep, 2008 - 05:47 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:30AM

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