2 simple questions for an expert ;)

Posted on 2003-03-10
Medium Priority
Last Modified: 2010-04-01
Hi, I'm new to programming in C++ and I have to make a program for school. Almost everything works, but I still have one problem: When I'm reading a string that contains spaces using scanf, it reads 2 different strings. I know that you can make it 1 string with '' quotes, but how do I READ such a thing (using scanf)? scanf("%s",&tempnaam); is what i have now.

The second question is an easy one for you die hards i think :)
I have to make a function that can print messages with colours.
Something like this:

void melding(char message[],color)
   char message[],color;

Then, I have to be able to call the function with a message and a color, like this:

melding("Data opgeslagen op schijf\n\nDruk op een toets om terug te keren naar het hoofdmenu",2);

That should print the message on screen in green.

I declared the funtion like this:

void melding(char[],char);

What am I doing wrong?

Thanks in advance!

PS, sorry for the amount of 10 points, but I'm new here and only have 75 points to spend ;)

Rgds, Frank
Question by:DA_MELKMAN
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Accepted Solution

substand earned 75 total points
ID: 8107165
you'll need to use getline(...) to skip the spaces.  scanf always terminates at a space.
LVL 10

Expert Comment

ID: 8107169
also, scanf is not c++, cin<< is
LVL 10

Expert Comment

ID: 8107182
regarding the second question, declare you're function like this:

void melding(char[], int);

you could also use char* instead of char[], if you want.

basically, the compiler is converting the "2" in your function to "chr(2);" which is some random control character.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8107375
Thanks for the comments substand, but we are not allowed to use cin, for our teacher doesn't want us to :/

Regarding the second question: I used int instead of char and now everything seems to work fine. Thanks :)
LVL 10

Expert Comment

ID: 8107582
no problem.  your teacher must be teaching the basics, because scanf is purely c, not c++.  Of course it still works, because most c/c++ compilers will work with both, so have no problem generating both.

however, what I said about scanf is still true.  to my knowledge, you can't get it to skip the spaces.  you'll need to use getline(...) as the function.  that will get an entire line of input.

also, you could loop through getchar(); (also a c function, but since you are using it otherwise, it should be ok.)

getchar loop would work like:

char ch;
printf("prompt the user: ");
ch = getchar();

while(ch != '\n')
        //append ch to your string
     ch = toupper(getchar());

Expert Comment

ID: 8109636
For ur first question u got format specifier in scanf where u can specify the characters to be accepted or specify the characters not to accept. The solution to ur problem is use like this:

#include <stdio.h>

void main()
     char str[80];
This will get a string until return key is pressed or the line ends.
But for u second question i need to see the code written inside the textcolor function........if u post that i can try out with ur code.
Eka S
LVL 30

Expert Comment

by:Mayank S
ID: 8110177
Since you're using C++, use getline () for reading strings, as:

char str[80] ;
cin.getline ( str, 80 ) ;

To correct the error in your function definition, give your color variable a data-type. You've written:

>> void melding ( char message[], color )

Make it:

void melding ( char message [], char color )

Your declaration [ void melding ( char [], char ) ; ] is correct.


Author Comment

ID: 8110484
Thanx for all comments !!!
First: Eka, your method didn't work for me, because the submenu for adding teams is accessed from the main menu, and for some reason the enter given there, will directly be taken in the add teams menu, creating a team with name '' :) (same goes for gets("blabla");)

Mayankeagle, thanks for the options. the messages in color work good now :)

But the points go to Substand, who in first instance proposed to use cin. My teacher agreed with that, so now I use cin.getline now

Thanks all!

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question