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

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




a few questions..on bloodshed dev c++..

 
Reply to this topicStart new topic

a few questions..on bloodshed dev c++..

triplecode hammerblow
post 3 Aug, 2007 - 01:46 AM
Post #1


New D.I.C Head

*
Joined: 5 Mar, 2007
Posts: 7


My Contributions


Hey people,

I've coding for sometime now..but it's a little confusing using bloodshed's dev c++..can you tell me if this happens on all compilers?

CODE


int main()
{
char words[10],*char_pointer;

cout<<"enter words";
cin>>words;
char_pointer=words;
cout<<char_pointer;
return 0
}


According to my textbook it's supposed to print the contents of the array...but it doesnt..it doesnt do it,if there's a space in between?!?! it stops at the space..but it prints the contents when I specify the sentence in the program crazy.gif

And....can I compare spaces??...I need to print the words of a sentence in the reverse order....i'm trying to break up the sentence by stopping at white spaces...but it doesnt seem to compare spaces blink.gif

This post has been edited by triplecode hammerblow: 3 Aug, 2007 - 03:49 AM
User is offlineProfile CardPM

Go to the top of the page

ajwsurfer
post 3 Aug, 2007 - 05:39 AM
Post #2


D.I.C Regular

Group Icon
Joined: 24 Oct, 2006
Posts: 292



Thanked 2 times

Dream Kudos: 50
My Contributions


I used the Cygwin shell in the Gnu compiler in Windows and got the same result. This is a characteristic of the language, not the compiler.

When thinking about this I could see how this is the characteristic of the "cin" statement. The "cin" will stop at the first whitespace. A little bit of experimenting will go a long way to showing you what is really going on here:
CODE

#include <iostream>

using namespace std;

int main()
{
  char words[11]; //= {"here it is"};
  char *char_pointer;

  cout<<"enter words: ";
  cin>>words;
  //:cin>>words;
  
  char_pointer=words;
  cout<<char_pointer;

for(int i = 0; i < 11; i++)
   cout << words[i];
  
  return 0;
}

wink2.gif
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 3 Aug, 2007 - 05:44 AM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


You can use the .getline() method to get multiple words.
User is online!Profile CardPM

Go to the top of the page

Pontus
post 3 Aug, 2007 - 10:32 AM
Post #4


Dreaming Coder / Coding Dreamer

Group Icon
Joined: 28 Dec, 2006
Posts: 527



Thanked 2 times

Dream Kudos: 275
My Contributions


u only get 1 word.
Use this
CODE

for(int a=0;a<10;a++)
cin>>words[a];

instead of
CODE
cin.get();

and use this too
CODE

for(int a=0;a<10;a++)
{
cout<<words[a]<<endl;
if(a<9)
cout<<' ';
}

To display them
If u dont understand some of this code tell me.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 3 Aug, 2007 - 10:53 AM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


The user is saying that he is unable to get multiple words - it is due to the limitation of the cin object with regards to a whitespace. simply using cin.getline() will allow multiple word entry.
User is online!Profile CardPM

Go to the top of the page

triplecode hammerblow
post 3 Aug, 2007 - 11:44 PM
Post #6


New D.I.C Head

*
Joined: 5 Mar, 2007
Posts: 7


My Contributions


oh..okay thanks guys. smile.gif
User is offlineProfile CardPM

Go to the top of the page

JMFitz190
post 25 Jan, 2008 - 08:12 AM
Post #7


New D.I.C Head

*
Joined: 10 Jan, 2008
Posts: 5


My Contributions


QUOTE(triplecode hammerblow @ 3 Aug, 2007 - 02:46 AM) *

Hey people,

I've coding for sometime now..but it's a little confusing using bloodshed's dev c++..can you tell me if this happens on all compilers?

CODE


int main()
{
char words[10],*char_pointer;

cout<<"enter words";
cin>>words;
char_pointer=words;
cout<<char_pointer;
return 0
}


According to my textbook it's supposed to print the contents of the array...but it doesnt..it doesnt do it,if there's a space in between?!?! it stops at the space..but it prints the contents when I specify the sentence in the program crazy.gif

And....can I compare spaces??...I need to print the words of a sentence in the reverse order....i'm trying to break up the sentence by stopping at white spaces...but it doesnt seem to compare spaces blink.gif



I use the same Compiler. I'm sure you know to include "#include<iostream.h>"

As other people have stated, use: "cin.getline()" for multiple words.

Below I have taken your code and changed minor things. You still enter only one word to be printed to the screen.

CODE


//dreamincode.net

#include <iostream.h>
int main()
{

char words[10],*char_pointer;

cout<<"enter a word";
cin>>words;

char_pointer=words;
cout<<char_pointer;

system("Pause");
return 0;

}


Hope that helps a little.
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 25 Jan, 2008 - 02:59 PM
Post #8


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


Yay for pulling out a 5 month old topic... smile.gif

And, just to be nitpicky about your code, #include <iostream. is deprecated in favour of #include <iostream>.

On a more personal note, I would change system("PAUSE"); to cin.get();. It's more OS portable that way. smile.gif

:-P
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:07AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month