Plain C: Passing a static string into a user defined function

+ Modifying a char array (string)

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 2498 Views - Last Post: 12 April 2010 - 05:02 PM Rate Topic: -----

#1 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Plain C: Passing a static string into a user defined function

Posted 11 April 2010 - 06:13 PM

Plain C:
Below is my current working code, but it is poorly done, and i dont like doing it this way. So i was wondering if its possible to pass through a static string. As when ever I try it just doesn't work! Or able to change a char array.... I tried doing some strcpy but that just was more messy and more work (and didnt quite work)....
char checkint(char printer[])
{

int returner = 0;
 do 
 {
 printf("%s",printer[]);
  fflush (stdin); /*Clears the keyboard buffer. No special #include needed!*/
  scanf("%i", &returner);
 }
 while (returner == 0);
return returner;
}

int main()
{
int term=0,dobd=0;
char strt[]="How long is the term? ";
term=checkint(strt);

char strt0[]="What day were you born? ";
dobd=checkint(strt0);
return 0;
}



Static String:
int main()
{
int term=0,dobd=0;
term=checkint("How long is the term? ");
dobd=checkint("What day were you born? ");
return 0;
}



Changing char array:
int main()
{
int term=0,dobd=0;
char strt[50]="How long is the term? ";
term=checkint(strt);

strt="What day were you born? ";
dobd=checkint(strt);
return 0;
}



My actual code has allot of questions being asked and allot of data needing to be validated and such. SO having it really nice and clean is how i wanted it..... But my current code is somewhat messy and making allot of variables where there should only be the need for one or even none.

Thanks in advance :)

Sorry if my post is slightly unclear

This post has been edited by mattisdada: 11 April 2010 - 07:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Plain C: Passing a static string into a user defined function

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Plain C: Passing a static string into a user defined function

Posted 11 April 2010 - 06:20 PM

why not just pass char pointers ?
#include <iostream>

using namespace std;

int checkint(char *printer)
{

	int returner = 0;
	while(returner == 0)
	{
		printf("%s",printer);
		fflush (stdin);
		scanf("%i", &returner);
	}
	return returner;
}

int main()
{
	int term, dobd;
	term = checkint("How long is the term? ");
	dobd = checkint("What day were you born? ");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Plain C: Passing a static string into a user defined function

Posted 11 April 2010 - 06:28 PM

fflush(stdin) results in undefined behavior and you should not use it. Use
int c;
while ((c = getchar()) != '\n' && c != EOF) ; // Eat rest of data from stdin


If you don't like 's option, you could change to this:
int main()
{
    int term=0,dobd=0;
    char strt[50];
    strcpy(strt, "How long is the term? ");
    term=checkint(strt);

    strcpy(strt, "What day were you born? ");
    dobd=checkint(strt);
    return 0;
}


Was This Post Helpful? 1
  • +
  • -

#4 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Plain C: Passing a static string into a user defined function

Posted 11 April 2010 - 06:51 PM

Neither solution seems to work.

Strcpy it still fails to pass in the values. And the pointer just makes it break, also, apparently there is no function called iostream.h

Both give the error "Error: Passing argument 1 of 'checkint' makes integer from pointer without a cast...

Im not entirely sure how the pointer thing works, i tried doing it (but i didnt really know what i was doing) it still failed....
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Plain C: Passing a static string into a user defined function

Posted 11 April 2010 - 07:00 PM

our code compiles fine, either your using a out of date compiler (if your using iostream.h then that leads me to believe that). But post your code

EDIT: crap your doing this in C, sorry. Just change the headers

does this work?
#include <stdlib.h>
#include <stdio.h>

int checkint(char *printer)
{

        int returner = 0;
        while(returner == 0)
        {
                printf("%s",printer);
                fflush (stdin);
                scanf("%i", &returner);
        }
        return returner;
}

int main()
{
        int term, dobd;
        term = checkint("How long is the term? ");
        dobd = checkint("What day were you born? ");
        return 0;
}


This post has been edited by ImaSexy: 11 April 2010 - 07:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Plain C: Passing a static string into a user defined function

Posted 11 April 2010 - 07:03 PM

I am using Quincy 2005 with C99 support turned on.

My main code is at home (im out and about right now)

But using jackofalltrades:
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int checkint(char printer)
{

        int returner = 0;
        while(returner == 0)
        {
                printf("%s",printer);
                int c;
				while ((c = getchar()) != '\n' && c != EOF) ; // Eat rest of data from stdin
                scanf("%i", &returner);
        }
        return returner;
}
int main()
{
    int term=0,dobd=0;
    char strt[50];
    strcpy(strt, "How long is the term? ");
    term=checkint(strt);

    strcpy(strt, "What day were you born? ");
    dobd=checkint(strt);
    return 0;
}




Or using yours
//#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>

int checkint(char *printer)
{

        int returner = 0;
        while(returner == 0)
        {
                printf("%s",printer);
                int c;
				while ((c = getchar()) != '\n' && c != EOF) ; // Eat rest of data from stdin
                scanf("%i", &returner);
        }
        return returner;
}
int main()
{
	int term=0, dobd=0;
	term = checkint("How long is the term? ");
	dobd = checkint("What day were you born? ");
	return 0;
}





Unfortunately i cant change compiler.... So if it is an issue with my compiler, i'm not allowed to use another one. Has to be good ol quincy 2005.....

This post has been edited by mattisdada: 11 April 2010 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Plain C: Passing a static string into a user defined function

Posted 11 April 2010 - 07:06 PM

Did you try the code i posted? it should compile fine
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Plain C: Passing a static string into a user defined function

Posted 11 April 2010 - 07:06 PM

This:
int checkint(char printer)

is looking to receive a SINGLE character, not an array of them.

EDIT: And the while (c = getchar()) thing goes AFTER the scanf. So that it eats the rest of the data entered.
Was This Post Helpful? 1
  • +
  • -

#9 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Plain C: Passing a static string into a user defined function

Posted 11 April 2010 - 07:11 PM

View PostJackOfAllTrades, on 12 April 2010 - 11:06 AM, said:

This:
int checkint(char printer)

is looking to receive a SINGLE character, not an array of them.

EDIT: And the while (c = getchar()) thing goes AFTER the scanf. So that it eats the rest of the data entered.

//#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>

int checkint(char printer[])
{

        int returner = 0;
        while(returner == 0)
        {
                printf("%s",printer);
                int c;
				while ((c = getchar()) != '\n' && c != EOF) ; // Eat rest of data from stdin
                scanf("%i", &returner);
        }
        return returner;
}
int main()
{
    int term=0,dobd=0;
    char strt[50];
    strcpy(strt, "How long is the term? ");
    term=checkint(strt);

    strcpy(strt, "What day were you born? ");
    dobd=checkint(strt);
    return 0;
}




Seemed to work. That is preety nice. Although i would prefer to get that pointer option to work..... (its clearer to read)

EDIT: Also, why does that code you made work? Its very complex and unclear to me....
EDIT2: I really have no idea what its doing, why is it working when the while, isnt doing anything (has no braces)... What EOF??? Sorry I am new to C. Im used to more user friendly languages like PHP and Python....
EDIT3: Imasexy: Your modified code still didn't work unfortunately :(

This post has been edited by mattisdada: 11 April 2010 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Plain C: Passing a static string into a user defined function

Posted 12 April 2010 - 05:37 AM

OK, what this code
int c;
while ((c = getchar()) != '\n' && c != EOF) ; // Eat rest of data from stdin

does is very simple...it does what you need it to do! :)

Here's the deal:

Unless you're using some special console input handling library, your program will not accept input from the keyboard until the user presses Enter, right? So when you ask the user to enter something and you retrieve it via scanf(or other input functions), the Enter key -- in the form of the newline ('\n') character -- is put into the input buffer as well and will stay there until it's removed somehow.

Here's an example. You ask the user for an integer, and they enter
1313 Mockingbird Lane
followed by the Enter key. The scanf will read the 1313 into your variable; however, that's going to leave in the buffer the following:
 Mockingbird Lane\n

If you do not get rid of it, the next call to an input reading function will use that as its input, and things will get all out of whack.

That little chunk of code just loops continuously reading each character left in the input buffer into the variable c -- and discarding it -- until it's read that final '\n' or reaches EOF (in the case that instead of hitting Enter they use the EOF key combo, which is Ctrl-D on Unix and Ctrl-Z on Windows, or the data is being read from a file ... EOF means "End Of File").
Was This Post Helpful? 0
  • +
  • -

#11 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Plain C: Passing a static string into a user defined function

Posted 12 April 2010 - 05:56 AM

Wow, thank you. Your code makes ALLOT of sense now! Thanks :)

I am amazed you took the time to explain that so well. And how simple the code is, even thought it looks highly complex...(To me anyway...)

But is it possible to use pointers to solve my problem? Or should i just leave it as is.....?

This post has been edited by mattisdada: 12 April 2010 - 05:57 AM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Plain C: Passing a static string into a user defined function

Posted 12 April 2010 - 06:06 AM

What do you mean about the pointers? There's no reason you can't use 's method of just passing the string to the function:

int checkint(char *prompt)
{
    printf("%s ", prompt);
    // get integer
}
val = checkint("How long is the term?");


That's the way I would do it in this case; I just thought you wanted a single buffer to hold the string.
Was This Post Helpful? 0
  • +
  • -

#13 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Plain C: Passing a static string into a user defined function

Posted 12 April 2010 - 06:16 AM

View PostJackOfAllTrades, on 12 April 2010 - 10:06 PM, said:

What do you mean about the pointers? There's no reason you can't use 's method of just passing the string to the function:

int checkint(char *prompt)
{
    printf("%s ", prompt);
    // get integer
}
val = checkint("How long is the term?");


That's the way I would do it in this case; I just thought you wanted a single buffer to hold the string.

I just want the least amount of lines of code for readability....
But even using your code (after fixed up slightly, missing a return function, and the var bit wasnt in a function....) still didnt work... At least in my compiler, just gives me the error:"Error: Passing argument 1 of 'checkint' makes integer from pointer without a cast...."
Im perfectly fine for the way it works(well it works!)... But I would prefer to have it readable and simple.... As others have to read it.... So i can just list checkint("stuff to say"); one liners are nice....
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Plain C: Passing a static string into a user defined function

Posted 12 April 2010 - 06:26 AM

I don't think you're completely paying attention. Are you missing the asterisk on the argument to your checkint function as you did earlier??? It MUST look like this:

int checkint(char *prompt) { } 


that * is VERY IMPORTANT!
Was This Post Helpful? 0
  • +
  • -

#15 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Plain C: Passing a static string into a user defined function

Posted 12 April 2010 - 06:29 AM

Yeah I did.

I copied and pasted your code to....

It just keeps erroring.....
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2