Reputation: 49
I'm trying to exit a while loop within a function but I don't think i'm writing the pointer correctly, c is new to me and reading examples online is confusing.
I would appreciate feed back on what I'm writing incorrectly.
int logOn(int *log);
int main(void)
{
int log;
log = 1;
while(log = 1)
{
int logOn(log);
}
while(log = 2)
{
int mainMenu();
}
printf("\ngood bye");
}
int logOn(*par)
{
char p;
printf("would you like to log on (y/n): ");
scanf("%s", &p);
if(p="y")
{
par = 2;
}
else if(p="n");
{
par = 3;
}
else
{
printf("\nError entering command\n")
}
return 0;
}
I've updated my code and fixed a lot of the errors you guys helped my identify.
now for some reason it goes through my logon loop twice if i hit a key neither 'y' or 'n'
this is what my current code looks like
int logOn(int *log);
int mainMenu(int *log);
int main(void)
{
int log;
log = 1;
while(log == 1)
{
(void) logOn(&log);
}
while(log == 2)
{
mainMenu(&log);
}
printf("\ngood bye");
}
int logOn(int *par)
{
char p;
printf("would you like to log on (y/n): ");
scanf("%c", &p);
if(p == 'y')
{
*par = 2;
}
else if(p == 'n')
{
*par = 3;
}
else
{
printf("\nError entering command\n");
}
return 1;
}
int mainMenu(int *par)
{
printf("\nMain Menu\n");
*par = 3;
return 0;
}
and this is what it is out putting when i dont hit 'y' or 'n'
would you like to log on (y/n): e
Error entering command
would you like to log on (y/n):
Error entering command
would you like to log on (y/n):
Upvotes: 0
Views: 106
Reputation: 1122
Some points in your program .
First, while(log = 1)
, since your are checking if the log is equal to 1 or not, the syntax should be while(log == 1)
{
}
Secondly, while calling the logOn
function, pass the address of log variable (int logOn(&log)
;)
Third, The signature of the int logOn(*par)
function should be int logOn(int *par)
.
Fourth, "y"
or "n"
represents a string literal, if you want to use only a single character, use 'y' or 'n', that is with single quote.
Fifth, To change the value of a pointer use the de-referencing operator *
.
Your line in the code should be
*par = 2; (//Content of par equal to 2)
*par = 3;
Happy programming.
Upvotes: 1
Reputation: 3721
while(log = 1)
while(log = 2)
Wrong. In c, the =
operator is for assignment, not comparison. You are setting log equal to 1 and 2 there. You want the ==
operator for comparison:
while(log == 1)
Next:
scanf("%s", &p);
Wrong. %s
is for arrays of char
ending in zero ('\0'). This is how c represents strings. For a single character, you want
scanf("%c", &p);
Next:
if(p="y")
You are misusing the assignment operator again. In addition, you are comparing a p
, a char
to "y"
, a char *
- char array. Double quotes are for strings, single quotes/apostrophes are for single characters. You are looking for
if(p == 'y')
Next:
else if(p="n");
Same errors as above, plus, the semicolon - ;
doesn't belong there. Suffice it to say, that should cause a compilation error on your else
statement below. You want:
else if(p == 'n')
Next:
par = 2;
par = 3;
You are assigning integers to a pointer. You probably mean
*par = 2;
*par = 3;
Next:
int logOn(log);
should be
(void) logOn(&log);
Next:
int mainMenu();
I don't know what mainMenu
is, but the int
keyword makes no sense there.
Upvotes: 1
Reputation: 31
The parameters of your logOn function must be defined as so:
int logOn(int *par){//function body}
Also the syntax of your while condition must be as follows:
while(log == 1)
Note the double == sign. This is necessary as a single = sign will result in an infinite loop.
Furthermore when you pass in the address of a variable into a function as you have, then a * operator is required in front of it within the function body in order to deference the value at that address.
e.g
int logOn(int *par)
{
printf("%p", par); //address par
printf("%d",*par); //value stored at address par
}
Hope this helps.
Upvotes: 3
Reputation: 8546
while(log == 1)
it's ==
not =
.
When in a while condition you write log = 1
it simply assigns 1
to log
. Then while
gets 1
from the variable log
, which is implicitly converted to true
, which makes while
loop infinitely.
Upvotes: 3