Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,226 members, 7,818,770 topics. Date: Monday, 06 May 2024 at 01:37 AM

Abeg Guys What Is Wrong With This C Code - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Abeg Guys What Is Wrong With This C Code (1202 Views)

Help! Im Stuck With This C# Program / Please Programmars Help Me Out With This C# Program! I Have Test Tommorrow!!!!! / Programmer Should Please Help Me To Solve This C++ Questions (2) (3) (4)

(1) (Reply) (Go Down)

Abeg Guys What Is Wrong With This C Code by Nobody: 9:08pm On Feb 12, 2010
I was just goofing around with this code, am still a novice in C and fighting to learn, this programme is just a programme that accepts a username and password, checks if it is correct and then calls up a function that calculates an area of a circle, i know I can just do it straight. PLZ DON'T LAUGH AT MY IGNORANCE, just instruct me on what to do ,it dosen't compile;

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

/*string variable declaration*/
char usrname [10], pass [7];
char name = "dikachi";
char passr = "xerone";

/*variable and constant declaration*/
int rad;
const float pie = 3.142;

/*function declaration*/
int area(int rad);

/*main function statement*/
int main(int argc, char *argv[])
{
printf("enter the username and password to calculate the area of a circle\n"wink;
scanf("%s", usrname);
scanf("%s", pass);

/*control statement*/
if((name == usrname) && (passr == pass))
printf("the area of the circle is ", area (int rad));/*calling the function area*/
else
printf("ACCESS DENIED"wink;

system("PAUSE"wink;
return 0;
}

/*function definition*/
int area(int rad)
{
printf("what is the value of the radius!"wink;
scanf("%d", &rad);
return(pie * pow(rad, 2));
}
Re: Abeg Guys What Is Wrong With This C Code by iarm(m): 10:44pm On Feb 12, 2010
Firstly, without even looking at the code, are you linking with -lm?

Secondly, after looking at the code, you clearly have a misunderstanding of how arrays (and pointers) work.

Your "string variable declarations" are stupid. Redeclare them as so:

char usrname[] = "dikachi";
char pass[] = "xerone";

This automatically creates storage for your string, plus your null character, what you seem to have done is try to place a string in a redeclaration, which is a character (and not an array) in other words, trying to fit a string into a character, which has already been declared wink

The second error you should be getting deals with the usage of this redeclaration; if((name == usrname) && (passr == pass) -- you're not allowing this to work properly; Kindly read error messages before trying to ask questions about code, maybe they can give you some direction in future.

Do you have a book? If not, get "The C Programming Language, by Kernighan and Ritchie".
Re: Abeg Guys What Is Wrong With This C Code by holusormi(m): 6:18am On Feb 13, 2010
When u try 2 compile it, what line does it give d error ? Secondly,need i remind u that usrname and name ain't d same fing ? Neither is paswrd and pass d same fing. Also in your 'scanf' function,i can't see the '&' sign ! Did u throw it away?
Thirdly, in ur conditional statements,it shoulda been if((usrname==dikachi)&&(paswrd==xerone)) nat what u wrote.
Anyway check all again and recompile,  Hapi programming.
Re: Abeg Guys What Is Wrong With This C Code by Nobody: 5:25pm On Feb 13, 2010
@iarm

There are nicer ways of teaching people stuff.

@Movingcoil

I'm not sure exactly how this works, but I know that you cannot use the if statement to compare strings. Wherever you can, search for the functions strcmp and stricmp. They're included in string.h, so you won't have to change your include directives. I know they take two arguments each, which are the strings you want to compare and return 1 or 0 if they're equal or unequal or something like that. But it's not so straightforward, there are ASCII value considerations and all that. Search for a proper explanation of the two functions strcmp and stricmp and you'll have what you need.
Re: Abeg Guys What Is Wrong With This C Code by Nobody: 5:34pm On Feb 13, 2010
Plus, @iarm, your talk about linking with -lm and all that is dependent on your compiler. Writing a number when declaring an array of characters isn't illegal in C either. If it gives you errors, then that's thanks to your compiler. Plus, Movingcoil needs absolutely no knowledge of pointers. The program here is a very simple one.
Re: Abeg Guys What Is Wrong With This C Code by iarm(m): 9:46pm On Feb 13, 2010
@mactao Erm, it's usually a common misdemeanour. "Writing a number when declaring an array of characters isn't illegal in C either" I never said it was, "if it gives you errors, then that's thanks to your compiler". If your compiler is ANSI-C it's usually doing something against the rules specified by ANSI-C. But who cares, you should learn sentence construction -- it took me 4 passes to figure out what you were trying to say. Also If you're unable to learn something without me being nice to you, then you probably don't deserve to learn anyway,
Re: Abeg Guys What Is Wrong With This C Code by dueal(m): 10:35pm On Feb 13, 2010
@movingcoil. Long time no C. It's being long since i wrote in low-level C but i think i can help.
Your first error is on line 2 & 3. Trying to assign a string to a char, wrong.
Next, to compare C strings use strcmp() in your conditional(u can use the '==' only if both operands are string literals).
In your area() body the return keyword needs a space btw it and the parenteses.
Also, in your main() where if condition is true,the printf() has no place holder(%d) for its second argument.
I also noticed that u #include standard headers with the .h suffix(#include <string.h>) this shouldn't work with ANSI C current standard instead do this(#include <whatever and no dot h>). I see u understand that the name of an array is a pointer to its first element(holusormi asked where u threw the '&' operator, hope he learns from this).Just an advice, Move to C++ if u don't want to bug your brains with malloc(), realloc(), name collisions,  e.t.c. Have fun!
Re: Abeg Guys What Is Wrong With This C Code by akenzuaII(m): 2:52am On Feb 14, 2010
First:

STOP!!

In ANSI-C - there is no concept such as strings - that is C++ and SHOULD NOT be confused! (do not include string.h - see why below)

Strings in C is actually a null-terminated array of characters (a char is really an unsigned integer), to use it you normally declare a CONST array pointer that iterates through the array - 'CONST' as you do not want to modify your array.

As a programmer - please choose the right tool for the job, using C when this can be done using C++ is a mystery - but of-course you are learning so it's excusable. (dont get me started on people comparing C# and C++ and C and Java)

I will not point out your errors, once you have chosen the right starting point it will become clear - you can easily do this using C++ with the Standard Template Library (use the string and iostream library) and reformat your code - I may be wrong you may want to do something clever with C, just make sure you overhaul your code, this is poor form.

Generally - you use C for low level work, like writing interface code for periphirals, driver writing, compiler writing etc. Almost all operating systems have some C in them
C++ can be used for semi-high level procedural but it really shines in Object Oriented and Generic programming. The semi-high is my emphasis. Once you master C++ - you can master any other language out there (apart from C, Assembly or COBOL and some others so that renders my statement mute!)

Keep up the work and most of all - enjoy!
Re: Abeg Guys What Is Wrong With This C Code by akenzuaII(m): 2:55am On Feb 14, 2010
I just realised I wrote exactly the same as dueal!sorry

That will teach me for not reading the WHOLE post before-hand!
Re: Abeg Guys What Is Wrong With This C Code by akenzuaII(m): 3:03am On Feb 14, 2010
@ mactao

You can in fact use 'if' to compare strings (C++ here) - let me break it down:

1. If statement is looking for a BOOL - true or false,

2. string class overloads the operator== and returns a BOOL, therefore "paul"=="john" actually works

Of course the operands have to be of type string (or 'cast') but yes you can use 'if' to compare strings - good way of arguing in favour of 'overloading operators' in C++
Re: Abeg Guys What Is Wrong With This C Code by Nobody: 11:47pm On Feb 14, 2010
am very grateful for ur replies and right now am doing something about what you guys wrote, I main intention was learning c++ but just that I have to pass my ece 271 and 281 exams which is only c and the lecturers is like she is just googling her lecture note. I pray we finish the exams cos I hv a lot of things running in my brain that I want to do with C++. thanx again buddies, i'll love more suggestion and if someone can just rewrite the code for me since you already know what I want to do, i'll appreciate.
Re: Abeg Guys What Is Wrong With This C Code by Nobody: 11:49pm On Feb 14, 2010
am very grateful for ur replies and right now am doing something about what you guys wrote, I main intention was learning c++ but just that I have to pass my ece 271 and 281 exams which is only c and the lecturers is like she is just googling her lecture note. I pray we finish the exams cos I hv a lot of things running in my brain that I want to do with C++. thanx again buddies, i'll love more suggestion and if someone can just rewrite the code for me since you already know what I want to do, i'll appreciate.
Re: Abeg Guys What Is Wrong With This C Code by stya: 10:07am On Feb 15, 2010
I think you should use the inbuilt function for comparing the two string. The function is strcmp(dest, source, length);

the value of rad pass to the area function will still remain zero since you haven't initialize the var.

You may need to go for Denie R book as stated above to get familiar with the C syntax.
Re: Abeg Guys What Is Wrong With This C Code by Nobody: 7:49pm On Feb 15, 2010
iarm:

@mactao Erm, it's usually a common misdemeanour. "Writing a number when declaring an array of characters isn't illegal in C either" I never said it was, "if it gives you errors, then that's thanks to your compiler". If your compiler is ANSI-C it's usually doing something against the rules specified by ANSI-C. But who cares, you should learn sentence construction -- it took me 4 passes to figure out what you were trying to say. Also If you're unable to learn something without me being nice to you, then you probably don't deserve to learn anyway,

@iarm

Let your hair down. Relax. People disagree everyday and on every thread on Nairaland. You shouldn't take a shot at my English because of that. And just for the record, I speak and write flawless English.

If everyone you met in your life was this mean to you, you wouldn't be where you are today. Just try and be nicer is all I was trying to say. The sentences I wrote there weren't complicated, and you had to write the "4 passes" thing. I get the feeling you just wanted to say something nasty to me. You're very welcome. Other people you meet may not be willing to take the things you're doling out to Movingcoil and me.

Enjoy!
Re: Abeg Guys What Is Wrong With This C Code by Nobody: 4:29am On Feb 16, 2010
@mactao, I read the post, I don't think he wrote that maliciously or with the intention of wat you have in mind, there might be slight misunderstanding somewhere.@me, talking to me nicely and not talking to me nicely, I can't compare when you are ignorant and continue making same mistake everytime, the problem here is that am still studying C newly and haven't reached the chapter of arrays, so maybe would understand better when I reach there, anybody can say anything to me as far as you are more knowledgeable than me in this context.
thanx buddies again.
Re: Abeg Guys What Is Wrong With This C Code by Ghenghis(m): 3:03pm On Feb 16, 2010
Movingcoil:

everytime, the problem here is that am still studying C newly and haven't reached the chapter of arrays, so maybe would understand better when I reach there

Thats the classical beginner problem, can't wait to write code.

I'm the opposite, when learning something new(languages or frameworks) , i usually go through the manual first and understand the nitty gritty. Any code i write is done in a note pad.

Also get a good C book and practice ONLY the exercises in the book, it would guide you through the language rather than you hitting your foot/head on every rock in your way.
When i was in school there used to be turbo C manuals everywhere (bus stops). Turbo C is still the best tool to learn C, it comes with a great debugger also. Keep it simple .
http://upload.wikimedia.org/wikibooks/en/f/f8/C_programming.pdf
Re: Abeg Guys What Is Wrong With This C Code by sunnyben: 7:04pm On Feb 16, 2010
That's very of you.

I believe in practice as i'm reading especially programming ebooks

I will suggest you get an IDE (integrated development environment) like DEV-C++ from http://www.bloodshed.net.
It will help you spot where the errors are.

And don't rely only on what you're taught in school. get good ebooks - there are many of them online.

Concerning your C program:

1. the characters must be declared as arrays to handle strings i.e char name[]
2. A function call requires only a variable that is already declared i.e area(rad), not area(int rad)
With this the program will compile but not run perfectly
3. Arrays cannot be compared literally, use function strcmp(string,string). The function returns 0 if true.
I used this algorithm, it's simple logic if(!(strcmp(pass, passr) || strcmp(usrname, name)))

4. there is no placeholder inside the printf function i.e.

printf("the area of the circle is %d ", area(rad));

That's all that's wrong with the code
Don't worry, you have a great future!
Re: Abeg Guys What Is Wrong With This C Code by chukxy: 4:14pm On Feb 24, 2010
@ poster. If you have not got the solution before now, here is the corrected code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

/*string variable declaration*/
char usrname[], pass[];
char *name = "dikachi";
char *passr = "xerone";

/*variable and constant declaration*/
int rad;
const float pie = 3.142;

/*function declaration*/
int area(void);

/*main function statement*/
int main(int argc, char *argv[])
{
printf("enter the username and password to calculate the area of a circle\n"wink;
scanf("%s", usrname);
scanf("%s", pass);

/*control statement*/
if((strcmp(name,usrname)==0)&&(strcmp(passr,pass)==0)){
//if((name == usrname) && (passr == pass))
printf("the area of the circle is:%d\n ", area ());/*calling the function area*/
}
else
{
printf("ACCESS DENIED"wink;
}

system("PAUSE"wink;
return 0;
}

/*function definition*/
int area(void)
{
printf("what is the value of the radius!"wink;
scanf("%d", &rad);
return(pie * pow(rad, 2));
}


Please go through it carefully and learn where you made mistakes. Cheers!!
Re: Abeg Guys What Is Wrong With This C Code by chukxy: 9:35am On Mar 09, 2010
@Movingcoil! How are you doing? It has been a long time I heard from you.
I would want us to be sharing our experiences in C language as i am also new in C. By so doing, we get to learn more and faster. Hope to hear from you soon.
Re: Abeg Guys What Is Wrong With This C Code by Nobody: 3:27pm On Apr 07, 2010
@chuckxy, I just discovered that c++ is easier and more object oriented than c so am diverting to c++, just looking for a good book on it.

(1) (Reply)

Video Tutorials On SQL, For Free / Learning Python, Using Python At Work/college: Must-try Best Python Quizzes / Build JAVA ENTERPRISE APPLICATIONS From Scratch With Spring Framwork

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 53
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.