September 17th, 2012, 09:29 AM
-
Clarification Needed
Can someone check the output of the program for sex='f', qual='p' and exp=0-9...
#include<stdio.h>
void main()
{
int exp;
char sex,qual;
printf("Enter the Sex, Qualification and Experience of the candidate");
scanf("%c %c %d",&sex,&qual,&exp);
if(sex=='m'&&qual=='p')
{
if(exp>=10)
printf("Salary is 15000");
else
printf("Salary is 10000");
}
else
{
if(sex=='m'&&qual=='g')
{
if(exp>=10)
printf("Salary is 10000");
else
printf("Salary is 7000");
}
else
{
if(qual=='p'&&exp>=10)
printf("Salary is 12000");
else
{
if(qual=='P')
printf("Salary is 10000");
else
{
if(qual=='g'&&exp>=10)
printf("Salary is 9000");
else
printf("Salary is 6000");
}
}
}
}
}
September 17th, 2012, 09:57 AM
-
1. Your indentation is rubbish, and you didn't use tags when posting code. It should look like this:
Code:
#include<stdio.h>
void main()
{
int exp;
char sex, qual;
printf("Enter the Sex, Qualification and Experience of the candidate");
scanf("%c %c %d", &sex, &qual, &exp);
if (sex == 'm' && qual == 'p') {
if (exp >= 10)
printf("Salary is 15000");
else
printf("Salary is 10000");
} else {
if (sex == 'm' && qual == 'g') {
if (exp >= 10)
printf("Salary is 10000");
else
printf("Salary is 7000");
} else {
if (qual == 'p' && exp >= 10)
printf("Salary is 12000");
else {
if (qual == 'P')
printf("Salary is 10000");
else {
if (qual == 'g' && exp >= 10)
printf("Salary is 9000");
else
printf("Salary is 6000");
}
}
}
}
}
2. void main is just wrong. main returns an int.
> Can someone check the output of the program for sex='f', qual='p' and exp=0-9...
$ ./a.out
Enter the Sex, Qualification and Experience of the candidatef p 5
Salary is 6000$
$ ./a.out
Enter the Sex, Qualification and Experience of the candidatef P 5
Salary is 10000$
Perhaps examine the case of letters in your code.
September 17th, 2012, 10:18 AM
-
since your program compiles and runs, and since we have no further information, we cannot determine logic errors. See comments for improvements.
Code:
#include<stdlib.h> /***************** EXIT_SUCCESS defined in stdlib.h */
#include<stdio.h>
int main() { /***************** the operating system needs your program to return a deliberate status code */
int exp;
char sex,qual;
puts("Enter the Sex, Qualification and Experience of the candidate");
scanf("%c %c %d",&sex,&qual,&exp);
printf("sex(%c), qual(%c), exp(%d)\n",sex,qual,exp); /***************** inserted for exhibition */
if(sex=='m'&&qual=='p')
{
if(exp>=10)
puts("Salary is 15000");
else
puts("Salary is 10000");
}
else
{
if(sex=='m'&&qual=='g')
{
if(exp>=10)
puts("Salary is 10000");
else
puts("Salary is 7000");
}
else
{
if(qual=='p'&&exp>=10)
puts("Salary is 12000");
else
{
if(qual=='P')
puts("Salary is 10000");
else
{
if(qual=='g'&&exp>=10)
puts("Salary is 9000");
else
puts("Salary is 6000");
}
}
}
}
putchar('\n'); /***************** for exhibition */
return EXIT_SUCCESS; /***************** choose a value to return to the operating system */
}
Demonstration runs:
Code:
$ echo fp0 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(0)
Salary is 6000
$ echo fp1 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(1)
Salary is 6000
$ echo fp2 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(2)
Salary is 6000
$ echo fp3 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(3)
Salary is 6000
$ echo fp4 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(4)
Salary is 6000
$ echo fp5 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(5)
Salary is 6000
$ echo fp6 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(6)
Salary is 6000
$ echo fp7 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(7)
Salary is 6000
$ echo fp8 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(8)
Salary is 6000
$ echo fp9 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(9)
Salary is 6000
$ echo fp10 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(10)
Salary is 12000
$ echo fp11 | ./this_program
Enter the Sex, Qualification and Experience of the candidate
sex(f), qual(p), exp(11)
Salary is 12000
[edit]Your logic is probably awful, although we don't actually know the task. Let's guess the task is to handle male/female separately. A better design would be
Code:
if('m' == sex)
male(qual,exp);
else
female(qual,exp);
Better because you'd have some chance to handle all the cases. Better because you'd have less to think about in the smaller chunks. Better because the intent is clear.[/edit]
Last edited by b49P23TIvg; September 17th, 2012 at 10:25 AM.
[code]
Code tags[/code] are essential for python code and Makefiles!
September 19th, 2012, 02:58 AM
-
Salem thanks for pointing out the main error in my program that is the capital "P"...It was causing all the problems....
Brother b49P23TIvg and Salem I have recently started learning C so my logic might be awful. This program is about printing the salaries in case of males and females who have the qualifications graduate and post graduate and experience of less than 10 years or "10 years or more".
If you don't mind I need to ask some questions:
1. Why should I use int main even though my program works fine even with void main?
2. Brother b49P23TIvg what is this "puts"? Also EXIT_SUCCESS and putchar? I never saw them anywhere.
Also this statement, " printf("sex(%c), qual(%c), exp(%d)\n",sex,qual,exp);" is ambiguous to me.
3. if('m' == sex)
male(qual,exp);
else
female(qual,exp);
Can you elaborate this as well...
Please bear with me as I am quite new to programming.
September 19th, 2012, 07:50 AM
-
1. Why should I use int main even though my program works fine even with void main?
The operating system shell invokes your program, and that shell expects your program to return an integer status code. Shell scripts often use that value, as do other programs such as make.
In bash (use your internet browser to search for `man bash')
AND and OR lists are sequences of one of more pipelines separated by
the && and || control operators, respectively. AND and OR lists are
executed with left associativity. An AND list has the form
command1 && command2
command2 is executed if, and only if, command1 returns an exit status
of zero.
An OR list has the form
command1 || command2
command2 is executed if and only if command1 returns a non-zero exit
status. The return status of AND and OR lists is the exit status of
the last command executed in the list.
...
$? Expands to the exit status of the most recently executed fore‐
ground pipeline.
By god man don't you recall all those articles in Byte or PC World magazine back in the 1980's showing how people were figuring out how to write programs that at first could, in their DOS.bat file, prompt for y/n? and later discern multiple choice responses? They used the return code of int main() { blah; return STATUS; } .
2. Brother b49P23TIvg what is this "puts"? Also EXIT_SUCCESS and putchar? I never saw them anywhere.
Also this statement, " printf("sex(%c), qual(%c), exp(%d)\n",sex,qual,exp);" is ambiguous to me.
You might again test your browser on `EXIT_SUCCESS', `man puts'. EXIT_SUCCESS is a macro defined in stdlib.h that supplies the "program execution succeeded" status result in a portable manner. That is, in unix a return value of 0 indicates success while the other worldly operating system requires 666 to indicate success. You don't have to know all that to write portable code. You can always use EXIT_SUCCESS.
Code:
printf("a long string\n"); /*is inefficient because printf has to scan the format for conversion characters.*/
puts("a long string"); /*produces the same output without doing as much work.*/
fputs("a long string without the new line",stdout); /* get it? */
/* Unambiguous. Learn all the format specifiers. Except %i. Noone uses %i. */
printf("sex(%c), qual(%c), exp(%d)\n",sex,qual,exp);
printf("%s%c%s%c%s%d\n","sex(",sex,"), qual(",qual,"), exp(",exp); /* this might run faster---I'd certainly expect it to for sufficiently long strings---but it would crush programmers. */
3. if('m' == sex)
male(qual,exp);
else
female(qual,exp);
Keep reading your book. Eventually you'll run across functions and then you'll see the light. Meanwhile, note that you already do use functions. main is one that you've written, and you used a few: scanf and printf.
Last edited by b49P23TIvg; September 19th, 2012 at 07:52 AM.
[code]
Code tags[/code] are essential for python code and Makefiles!
September 19th, 2012, 08:42 AM
-
#include<stdio.h>
int main()
{
char gender,degree;
int experience,salary;
scanf("%c %c %d",&gender,°ree,&experience);
salary=(gender=='b'&°ree=='m'?
(experience>=10?printf("Salary is 15000"):printf("Salary is 10000")):
(gender=='b'&°ree=='g'?(experience>=10?printf("Salary is 10000"):printf("Salary is 7000")):
(degree=='m'?(experience>=10?printf("Salary is 12000"):printf("Salary is 10000")):
(degree=='g'?(experience>=10?printf("Salary is 9000"):printf("Salary is 6000")):printf("Love")))));
printf("%d",salary);
return EXIT_SUCCESS;
}
Thanks for the information. Can you tell me when I run this program why I get additional numbers in the output?
September 19th, 2012, 09:19 AM
-
Once again you've failed to state your expectation. Without that, I conclude that "the program works exactly as you've programmed it."
And please find, and use the "disable smilies" button.
Last edited by b49P23TIvg; September 19th, 2012 at 09:25 AM.
[code]
Code tags[/code] are essential for python code and Makefiles!
September 19th, 2012, 09:28 AM
-
See when I input b m 10 it gives output as 1500015, but as per my code it should be 15000. So where is the problem?
September 19th, 2012, 09:38 AM
-
Originally Posted by printf man page
RETURN VALUE
Upon successful completion, the fprintf() and printf() functions shall return the number of bytes transmitted.
strlen("Salary is 15000") is 15, which is assigned to salary, and then the statement right before return says
printf("%d",salary);
(You haven't put any line breaks so the digits all mush together. You were super lucky if your system prompt didn't overwrite the output.)
[code]
Code tags[/code] are essential for python code and Makefiles!
September 19th, 2012, 09:46 AM
-
Originally Posted by b49P23TIvg
strlen("Salary is 15000") is 15, which is assigned to salary, and then the statement right before return says
printf("%d",salary);
(You haven't put any line breaks so the digits all mush together. You were super lucky if your system prompt didn't overwrite the output.)
Okay fine but why doesn't this string length appear in the previous program which I posted?
September 19th, 2012, 10:35 AM
-
Please tell the forum you can tell the difference between these two statements.
Code:
salary=(gender=='b'&°ree=='m'?
(experience>=10?printf("Salary is 15000"):printf("Salary is 10000")):
(gender=='b'&°ree=='g'?(experience>=10?printf("Salary is 10000"):printf("Salary is 7000")):
(degree=='m'?(experience>=10?printf("Salary is 12000"):printf("Salary is 10000")):
(degree=='g'?(experience>=10?printf("Salary is 9000"):printf("Salary is 6000")):printf("Love")))));
salary=(gender=='b'&°ree=='m'?
(experience>=10?15000:10000):
(gender=='b'&°ree=='g'?(experience>=10?10000:7000):
(degree=='m'?(experience>=10?12000:10000):
(degree=='g'?(experience>=10?9000:6000):printf("Love")))));
[code]
Code tags[/code] are essential for python code and Makefiles!