• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

2 simple questions for an expert ;)

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
1 Solution
you'll need to use getline(...) to skip the spaces.  scanf always terminates at a space.
also, scanf is not c++, cin<< is
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.
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

DA_MELKMANAuthor Commented:
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 :)
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());
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
Mayank SAssociate Director - Product EngineeringCommented:
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.

DA_MELKMANAuthor Commented:
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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now