Jump to content
  • Advertisement
Sign in to follow this  
Nanook

not working as I expected [c/c++]

This topic is 3759 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My code is working allright, but when it asks user to input 1. value and I try to input eg. "n" it quits the program and if I enter any other char it starts over again.. I know thats what I want it to do in the main function, but why does it pass the value thats supposed to be read into the variable n1 to the ans variable?
#include <stdio.h>

void check_type(int n1,int n2,int n3)
{
	if ((n1 == n2) && (n1 == n3))
		printf("Your triangle is a equilateral. \n");
	else if ((n1 != n2) && (n2 != n3))
		printf("Your triangle is a scalene. \n");
	else
		printf("Your triangle is a isosceles \n");
	
	return;
}

bool check_valid(int n1,int n2,int n3)
{
	bool v;
	if ((n1 + n2) > n3)
		v = 1;
	else
	{
		printf("This is not a valid triangle \n");
		v = 0;
	}

	return(v);
}

void get_input(int &n1, int &n2, int &n3)
{
	do
	{
		printf("Pleas input 1. value: ");
		scanf("%d%*c", &n1);
		printf("Pleas input 2. value: ");
		scanf("%d%*c", &n2);
		printf("Pleas input 3. value: ");
		scanf("%d%*c", &n3);
	}while(!(check_valid(n1,n2,n3)));

	return;
}

int main()
{
	int n1,n2,n3;
	char ans;

	do
	{
		get_input(n1,n2,n3);
		check_type(n1,n2,n3);
		printf("Do you want to check another triangle? \n");
		scanf("%c%*c", &ans);
	}while(ans != 'n');
}


Share this post


Link to post
Share on other sites
Advertisement
What do you think scanf("%c%*c", &ans) does?
Hint: it's doesn't do what you think it does.

Not really much to do with C++, though.

Share this post


Link to post
Share on other sites
Hum.. well.. since you say it like that.. I guess it scans from a buffer or something and that it just use whats in there from the previous one.. I dont realy have a clue.. wanna explain?

Share this post


Link to post
Share on other sites
and do you realy mean "scanf("%s%*c", &ans)" or do you mean "scanf("%c%*c", &ans)" ? if you mean %s Im totaly lost :)

Share this post


Link to post
Share on other sites
I did mean '%c%*c', the above post has been corrected.

Quote:
Original post by Nanook
I dont realy have a clue


Since you don't really have a clue, why did you write it? The point of programming is solving problems, not throwing random lines of code at a problem.

In this particular situation, it would be best if you tried to find out on your own, for instance by perusing the scanf documentation.

Share this post


Link to post
Share on other sites
Well.. we haven't realy learned more than how to use scanf to read in a value at uni.. I guess we'll learn it later..

Share this post


Link to post
Share on other sites
btw, is there something in this code that I should do different or is it good programming?

Share this post


Link to post
Share on other sites
Yes.

1° Don't let "return;" as the last statement in a function: it's useless.

2° C tends to report errors with a non-zero integer return value.

3° C doesn't have references (you have to pass by pointer) or a bool keyword.

As such, your code would probably look like:

#include <stdio.h>

void check_type(int *n1,int *n2,int *n3)
{
if ((*n1 == *n2) && (*n1 == *n3))
printf("%s", "Your triangle is a equilateral. \n");
else if ((*n1 != *n2) && (*n2 != *n3))
printf("%s", "Your triangle is a scalene. \n");
else
printf("%s", "Your triangle is a isosceles \n");
}

int check_valid(int *n1,int *n2,int *n3)
{
if ((*n1 + *n2) > *n3) return 0;
printf("%s", "This is not a valid triangle \n");
return 1;
}

void get_input(int *n1, int *n2, int *n3)
{
do
{
printf("Pleas input 1. value: ");
scanf("%d", n1);
printf("Pleas input 2. value: ");
scanf("%d", n2);
printf("Pleas input 3. value: ");
scanf("%d", n3);
}
while(check_valid(n1,n2,n3));
}

int main()
{
int n1,n2,n3;
char ans;

do
{
get_input(&n1,&n2,&n3);
check_type(&n1,&n2,&n3);
printf("Do you want to check another triangle? \n");
scanf("%c", &ans);
}
while(ans != 'n');
}

Share this post


Link to post
Share on other sites
hmm.. ok.. we are allowed to use references from c++ tho.. so thats allright..

our teacher uses return; in the examples tho.. is it not needed?

Share this post


Link to post
Share on other sites
and.. he also tells us we should avoid having return in the middle of the function as you did here:


int check_valid(int *n1,int *n2,int *n3)
{
if ((*n1 + *n2) > *n3) return 0;
printf("%s", "This is not a valid triangle \n");
return 1;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!