ahamed101 40 Junior Poster

Hi,
I googled and found this... Hope it is not an offence to place links to other forums for information...

http://ubuntuforums.org/showthread.php?t=381626

Check that it might help you... The actual issue is there are some invisible characters... Good Luck...

ahamed101 40 Junior Poster

The same code compiled for me.
Platform HP-UX

Which is yours?

ahamed101 40 Junior Poster

Hi,
If you just want to read a string with space termination, you could just use scanf...
If you want to print the read characters, just find the length of the string read and print it...
You said the user input is arbitrary, in that case you can do
1) read each character and reallocate memory until enter is pressed
2) allocate certain chunk of memory and read the string, if the memory already allocated runs out, reallocate again...
In the above cases you can keep a track of the characters read...
If you are not sure about allocation and reallocation of the memory, please check here and here and here ...
There are examples at the bottom of how to use it...

Happy Coding...

ahamed101 40 Junior Poster

This code will given an run time error only if you have miss "&" in the scanf section...Please check your actual code if you have the &A and not just A in the scanf section...

ahamed101 40 Junior Poster

Please mark the thread as solved...
Thank You...

ahamed101 40 Junior Poster

scanf_s is some kind of secure version of scanf... its available in msdn...

ahamed101 40 Junior Poster

Please mark the thread as Solved...
Thank You...

ahamed101 40 Junior Poster

what is the series all about... is there any general formula for this?... give an example... like if n =3, output should be?...

ahamed101 40 Junior Poster

Try this...

switch(){
   case 1: 
               ;bool aaaa;
               break;
   ...
   ...
}

This will not give any error...

ahamed101 40 Junior Poster

what is the error that you are getting?...

ahamed101 40 Junior Poster

Move the for loop to the function you want... like...

int sum(int n){
int i = 0;
int total = 0;
int sum = 1;
	for(i=1; i<n; i++)
	{
		sum = sum + 3;
		total = total+sum;
	}
total++;

return total;

}

int main(int argc, char **argv)
{
	int i;
	int n;
	int sum = 1;
	int total = 0;
 
	printf("\n Please enter a positive integer : ");
	scanf("%d",&n);
	if(n <= 0){
		printf("\n Error!!!");
		return 1;
	}

total = sum(n);
	printf("\n Total %d",total);
	getchar();
	return 0;
}
ahamed101 40 Junior Poster

Yes it would do what you want... here just modified it to enable it for the user to enter the value...

int main(int argc, char **argv)
{
	int i;
	int n;
	int sum = 1;
	int total = 0;

	printf("\n Please enter a positive integer : ");
	scanf("%d",&n);
	if(n <= 0){
		printf("\n Error!!!");
		return 1;
	}

	for(i=1; i<n; i++)
	{
		sum = sum + 3;
		total = total+sum;
	}
	total++;/* To take care of 1 */
	printf("\n Total %d",total);
	getchar();
	return 0;
}
ahamed101 40 Junior Poster

You should not give that space at the end...

scanf("%d %d %d %d", &a1,&a2, &a3, &a4);
ahamed101 40 Junior Poster

Hi,
When I used scanf instead of scanf_s, it printed the output properly... I am not aware of scanf_s... googled it and found it out its some secure version of scanf or something... i use unix and this is available only for MS C I guess...

scanf("%d %d %d %d ", &a1,&a2, &a3, &a4);
ahamed101 40 Junior Poster

Yes that helps...

int main(int argc, char **argv)
{
	int i;
	int n = 3;
	int sum = 1;
	int total = 0;

	for(i=1; i<n; i++)
	{
		sum = sum + 3;
		total = total+sum;
	}
	total++;/* To take care of 1 */
	printf("\n Total %d",total);
	getchar();
	return 0;
}

Let me know if its work...

WaltP commented: Solving someone's homework for them -3
ahamed101 40 Junior Poster

Use continue instead so that you can enter the salary again...

i = 1;
	while(i <= numb_salaries){

		printf("Enter salary #%i: ",i);
		scanf ("%i", &salary);
		if(salary < 0){
			printf("\n Please enter a valid salary...\n Press enter to re-try...");
			fflush(NULL);
			getchar();
			continue;
		}
		sal_total = sal_total + salary;
		i++;
	}
ahamed101 40 Junior Poster

Hi

I am working on a program that computes the sum of N in increments of 3. I do not have any errors but when you run the program it does not compute correctly for example if you enter 2 for n the sum should be 5

#include "stdafx.h"
#include "stdio.h"

int
main(void)
{
int n, i, sum;
sum = 1;
printf(" Please enter a positive number\n");
scanf("%d", &n);
for(i=0; i<=n; i++);
{sum+=i;
}
printf("sum is %d", sum);
	  

	return 0;
}

You know what... we didn't get your question correctly... you said if the input is 2, output should be 5... it simply means you have to add 3 to n, n+3=output... is this what you want?
Or like you said increments of 3, as in... you give 2 out put should be (1+3)+(2+3)=9. if n is the input then (1+3)+(2+3)+(3+3)+(4+3)+(5+3)...+(n+3)=output, is this what you want?... Post the formula so that we can help you...

ahamed101 40 Junior Poster

Strings are passed by reference by default...
atoi-> Like salem said error handling is bit tough...
sscanf-> is what i would have used, it would separate as well as convert it to int format(or what ever format)...
strtol->Parses the C string str interpreting its content as an integral number of the specified base, which is returned as a long int value
long int strtol ( const char * str, char ** endptr, int base );

ahamed101 40 Junior Poster

How about a Scientific Calculator?

ahamed101 40 Junior Poster

how could I add something to the function so it calculates when and if n is negative

Yeah, negative factorial is wrong, why do you want that anyways?...

ahamed101 40 Junior Poster

Hi there,
Other than the syntax error, there is no issue with the logic... Below I am pasting the correct code with a print statement...

#include "conio.h"
#include "stdio.h"

int fib (int n); // function prototype//

int main (void)

{
	int num, result;
	printf ("Please enter a positive integer\n");
	scanf ("%d", &num);
	result= fib (num);
	printf("\n Fib : %d",result);
	getch();
	return (0);
}
int fib (int n)
{

	if ((n == 0) || (n == 1)) // stopping cases
	return 1;
	else // recursive case
	return fib(n-1) + fib(n - 2);
}
ahamed101 40 Junior Poster

Here it is...

#include "stdio.h"
#include "conio.h"
int fact (int n); // function prototype//

int main (void)
{
	int n,result;

	printf("please enter a positive integer");
	scanf("%d", &n);

	result = fact(n);

	printf("The answer is %d\n", result);

	getch();
	return 0;
}

int fact (int n)
{
	   if (n<=1)
		   return 1;
	   else
		   return(n*fact(n-1));
}
ssharish2005 commented: Point the mistake the OP did rather. Dont do his work! +0
ahamed101 40 Junior Poster

conio.h is not supported in Unix C. Are you using Unix C?
You must be using conio.h probably for getch() I guess, if so you may use getchar().