Sign in to follow this  

super newb question - printf_s & scanf_s

This topic is 3848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im simply trying to be able to type in a word and have the console spit it back out at me when i hit enter. here's what i have:
#include "stdafx.h"
#include <windows.h>

char *newtext[21];
char *newtext2[21];

int _tmain(int argc, _TCHAR* argv[])
{
	scanf_s("%s21", &newtext);
	printf_s("%21c", *newtext);
	scanf_s("%s21", newtext2);
	do
	{
	}while(NULL == NULL);
	return 0;
}
i know this is really bad, but i can't figure it out. it's a mess because i've been tinkering with it without success. what is the correct code for this? also, i know a do while loop isn't right for keeping the app open but i dont know how to do that either. i thought scanf would do trick, but it didn't. :( any help?

Share this post


Link to post
Share on other sites
FIRST of all, the do-while loop would be fine if you just said...

do { blahblahblah } while (true);

In other words (NULL == NULL) is the same as (true).


SECOND, you don't need that do-while loop to keep the app open. If you are in visual studio just "Run WIthoud Debugging" and it will leave the app open. I'm sure other IDEs have something similar.


THIRD, you have declared an array of character pointers for newtext and newtext2, not an array of characters (a string). You want to just have newtext be:

char newtext[21];

Then, the char* for the string will simply be the first element, &newText[0], or more commonly just newText (which is actually a pointer to the first element).

Basically, right now you are calling scanf_s and passing it an uninitialized char pointer--it doesn't point at any valid memory, or at least not on purpose.

Share this post


Link to post
Share on other sites
1)Unless you are going to explicitly allocate memory, the buffers for the input text should not be pointers, they should be normal arrays.

2) For your main loop (if it is particularly important to have a good one) you can keep a do-while or whatever, just declare a boolean to handle the user's desire to continue: ie: bool keepgoing = true; and have a certain string input (compare before spitting the output) which denotes the desire to quit. If said string is detected make keepgoing = false; (and the while should be while(keepgoing)) which would terminate the loop and by extension the program.

3) Other functions you might want to look into for string input are: gets() and fgets() -- gets() is often considered unsafe, but for this program it shouldn't be a big deal.

Don't sweat the scanf related problems. the scanf functions are very old and are widely accepted as troublemakers. I've certainly had my fair share of scanf related bugs over the years.

I hope this helps.

Share this post


Link to post
Share on other sites

This topic is 3848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this