I am to create a program in which the user puts in a word and the program displays the definition. The words are 20 words I select.

I have a comon idea on how to achieve this, but I never scaned for text before.

I know char is for a character.
I somehow cannot get declare a variable as a string. (it won't turn blue.) If this is correct, then I would suppose that it would scan like this

scanf_s("%s",word);

would it?

why don't you better use cin and cout?

either way, it would be like this: scanf("%s",&word);

>why don't you better use cin and cout?
Agreed. Perhaps this thread is in the wrong forum and he's really using C.

>either way, it would be like this: scanf("%s",&word);
No, it wouldn't. There are two glaring problems with that code:

1) Never ever ever use %s. It's no better than gets because you have no way of defending against buffer overflow. It's possible to make it safe by adding a field width, but by the time you go to all of the trouble, you might as well just have used fgets (or getline for C++).

2) word and &word are very different things when word is a pointer to char (and that happens to be the type that %s requires). If the argument to scanf is already a pointer, you would be wise not to use the address-of operator. If it's not a pointer, you need the address-of operator to make it a pointer.

>I somehow cannot get declare a variable as a string. (it won't turn blue.)
There's no built-in string type, so your editor will never tell you that you've declared one. You simply have to know what types are treated as strings. In C++ you should be using the std::string class declared in the <string> header. In C, the standard string type is an array of char with '\0' as the last character to mark the end of the string. In your case, it would be like this:

#include <stdio.h>

int main ( void )
{
  char my_string[100];

  if ( scanf_s ( "%99s", my_string, 100 ) == 1 ) {
    puts ( my_string );
  }

  return 0;
}

And you should be extremely careful with scanf_s. If scanf had surprising behavior, scanf_s just makes it worse. Even though scanf_s is "safe" according to Microsoft, I still recommend that you use fgets and sscanf.

>why don't you better use cin and cout?
Agreed. Perhaps this thread is in the wrong forum and he's really using C.

finally wee agree on something...

by the way... i'm not yet too familiar with using printf and scanf... since i've always worked with cin and cout... and i prefer using those because the other two are two tricky to use, and trigger lots and lots of problems...

>and i prefer using those because the other two are two tricky to use, and
>trigger lots and lots of problems...
That's a good reason.

you must be in a really good mood today... i had never seen two consecutive agreements with me in anything...

No, you're just on a roll. I'm sure you'll say something stupid eventually and things will be back to normal. ;)

ja!!! i'm on fire!!!!

>>I'm sure you'll say something stupid
>>eventually and things will be back to normal

Thank you... i'm flattered

I am still trying...i got it to accept words/string and displays the result, but shows the same result for any word....i am still working...i am modyfing the code some more... will post back

i get this error also:
error C2117: 'dog' : array bounds overflow

and nice little discussion you had above up there lol

Member Avatar for iamthwee

So are you using c++ or c? Posting some code would be helpful wouldn't it?

So are you using c++ or c?

c++....and also i am using now cout and cin

Member Avatar for iamthwee

Great, and your code thus far...

#include<iostream>
#include<string>
usingnamespace std;
int main ()
{
char apple[100];
char google [80];
cout<< "word to define: \n";
if ( scanf_s ( "%s", apple, 100 ) == 1 ) 
{
cout<<( "window's nemesis :O" );
}
if ( scanf_s ( "%s", google, 80 ) == 1 ) 
{
cout<<( "Go to yahoo " );
}
return 0;
}

i rechanged my code...i don't get the error anymore, but everything shows in order. if i put google first it shows me the definition of apple etc. I also tried the else state and the else if

Member Avatar for iamthwee

Ok, there's two options here, opt to use std::strings or char arrays.

std::strings preferred. Which is it gonna be?

if you recommend std::strings then i will go for it...but may I ask what are the differences or the pros and cons of each

Member Avatar for iamthwee

Yes I recommend it. I'll let someone who enjoys the semantics of the language to explain the nitty gritty.

BTW, I think you have your Post editor set to wysiwyg mode, Go into your control panel and change it to standard mode. It will take out all the parts coloured green etc in the code you just posted.

but how do i apply it? lol

Member Avatar for iamthwee

#include<iostream>
#include<string>
usingnamespace std;
int main ()
{
char apple[100];
char google [80];
cout<< "word to define: \n";
if ( scanf_s ( "%s", apple, 100 ) == 1 )
{
cout<<( "window's nemesis :O" );
}
if ( scanf_s ( "%s", google, 80 ) == 1 )
{
cout<<( "Go to yahoo " );
}
return 0;
}

Couldn't be simpler...

#include <iostream>
#include <string>

using namespace std;

int main (void)
{
string crap;
cout <<"Enter word:";
cin>>crap;

if (crap=="google")
{
cout <<"you got google";
}

etc

Couldn't be simpler...

#include <iostream>
#include <string>

using namespace std;

int main (void)
{
string crap;
cout <<"Enter word:";
cin>>crap;

if (crap=="google")
{
cout <<"you got google";
}

etc

I get the following:

conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Member Avatar for iamthwee

What compiler are you using?

I run Visual Studio 2005, write my code in C++, and press ctrl+f5.
the way the professor taught us.

ok i deleted one of my if statements and it works, but that is one word...what i was doing before is that i had one word with the statement you provided and repeated the statement with another word like this:

.....
if (word=="google")
{
cout <<"you got google";
}
if (word="yahoo")
{
cout<<"you got yahoo";
}
return 0;
}
Member Avatar for iamthwee

Visual studio, never used it.... Maybe you need a double equal sign in the second if clause.

Member Avatar for iamthwee

Also can you please take your post editor off wysiwyg mode.

Click on your control panel>edit options > scroll down to the bottom and change message interface to standard editor.

nope i don't know why it appeared like that there, but i think i fixed it...i accidentally typed in the cout as cout<<word=="you got google"

i will get back to you if it works, but so far i think i solved it with your great help


worked great, thank you for your help. Now i will do more research on std::strings

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.