Question

In: Computer Science

How would you call a username in a different function when coding in C? I have...

How would you call a username in a different function when coding in C?

I have a function that logs a user in, and I would like to say if (username == 'abc'){do function} but everytime I use the 'username' variable in a different function, it says it is an undeclared identifier.

Solutions

Expert Solution

Hello,

You can think of passing the identifier to a function after declaring it. Just assign the username of character string and pass it by value.

Below example would help you to understand and please let me know if you need more details.

void func(char *s){
//make use of the identifier passed from the main function

}

main()
{
char username[] = "USER_NAME";
func(username);
}

Happy Learning!

Adding answer based on your question:

No problem. Let me think of executing it as writable/readable text file in C programming. Just refer the below code snippet and customize accordingly.

int Login(){
char name[20];
printf("Enter UserName: ");
scanf("%s", name);
FILE * fp;
fp = fopen("c:\\temp\\userinfo.txt", "w");
fprintf(fp, name);
fclose (fp);
return 0;

}

int callingfunction() {

FILE *fp;
char str[MAXCHAR];
char* filename = "c:\\temp\\userinfo.txt";

fp = fopen(filename, "r");
if (fp == NULL){
printf("Could not open file %s",filename);
return 1;
}
while (fgets(str, MAXCHAR, fp) != NULL)
printf("%s", str);

// or store this in some other variable and execute your functionality
fclose(fp);
return 0;
}


Related Solutions

C-coding Question1: Fill in the function body for the provided sum() function such that the call...
C-coding Question1: Fill in the function body for the provided sum() function such that the call sum(g, i, j) returns the value of the calculation: Note: For the case where j < i, the function sum() should return 0. #include <stdio.h> int g(int val) { return val * val; } int sum(int (*f)(int val), int start, int end) { /* Your code goes here */ } int main() { printf("Result: %d\n", sum(g, 10, 20)); return 0; } ****************************************************** Question 2:...
Python Coding 1. validate_username_password(username, password, users): This function checks if a given username and password matches...
Python Coding 1. validate_username_password(username, password, users): This function checks if a given username and password matches in the stored users dictionary. This function returns True if a match found otherwise returns False. 2. validate_existing_user(users): This function asks for username and password and checks if user provided name and password matches. It prints an informational message and returns username if it does find a match. Call validate_username_password() function to perform this validation. A user has total of three chances to validate....
How would I fix my coding to allow the user to see what they have entered...
How would I fix my coding to allow the user to see what they have entered after they submit it? Where would I plug it into my code to see it? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="../signintrial.css"> <script> function loginHandler(){ // code for handing login event } function registerHandler() {    //checking phone number    if(phonenumber(document.getElementById('phone').value))    {    //when phone number is valid    document.getElementById('demo').innerHTML = document.getElementById('fname').value + " "...
In the case of c++, how would I store data drom a recursive function into a...
In the case of c++, how would I store data drom a recursive function into a 2D array?
How would I add a quickSort function to the below C++ code to sort the randomly...
How would I add a quickSort function to the below C++ code to sort the randomly generated numbers? #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int i; int array[10]; int odd; int Max; int counter = 0; int main() { cout << "The 10 random elements are: "; cout << endl; srand ( time(0) ); for (int j = 0; j < 99; j++) { i = rand() % 100; if (i != i - 1) array[j] =...
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
When would we call the Income Statement the Statement of Operations and when would we call...
When would we call the Income Statement the Statement of Operations and when would we call the Balance Sheet the Statement of Financial Position?
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
How would you correct this function in C to prevent buffer overflow using the fgets() function...
How would you correct this function in C to prevent buffer overflow using the fgets() function and strncat() function void nameBuilder() {    char fname[10];    char lname[10];    char fullname[20];    printf("Enter your first name: ");    scanf("%s", fname);    printf("Enter your last name: ");    scanf("%s", lname);    strcat(fullname, fname);    strcat(fullname, " ");    strcat(fullname, lname);    printf("Welcome. %s\n", fullname);    return; }
When would you exercise an American call option? When would you exercise an American put option?
When would you exercise an American call option? When would you exercise an American put option?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT