• Advertisement
Sign in to follow this  

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

This topic is 3583 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
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
Quote:
Original post by Nanook
hmm.. ok.. we are allowed to use references from c++ tho.. so thats allright..


Actually, it's not alright: you are currently writing C code which requires a C++ compiler—you're not using the advantages of C (portability on platforms where only C compilers exist) or the advantages of C++ (that is, writing non-C code), which is pretty much a waste.

The equivalent C++ code is not very different:

#include <iostream>

void check_type(int (&sides)[3])
{
if ((sides[0] == sides[1]) &&
(sides[0] == sides[2]))
std::cout << "Your triangle is a equilateral. \n";
else if ((sides[0] != sides[1]) &&
(sides[0] != sides[2]) &&
(sides[1] != sides[2]))
std::cout << "Your triangle is a scalene. \n";
else
std::cout "Your triangle is a isosceles \n";
}

bool invalid(int (&sides)[3])
{
for (int i = 0; i < 3; i++)
{
if ((sides + sides[(i+1)%3]) < sides[(i+2)%3])
return true;
}

return false;
}

void get_input(int (&sides)[3])
{
for (int i = 0; i < 3; i++)
{
std::cout << "Please input " << i + 1 << " value: ";
std::cin >> sides;
}
}

int main()
{
char ans;

do
{
int sides[3] = {0};
get_input(sides);

while (std::cin && invalid(sides))
{
std::cout << "Invalid triangle \n";
get_input(sides);
}

check_type(sides);

std::cout << "Do you want to check another triangle? \n";
}
while(std::cin >> ans && ans != 'n');
}



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


It's redundant: it would automatically be added by the compiler if you don't add it.

Share this post


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


This is a partially correct suggestion. The original suggestion is to never have two return statements in a function if that function is not smaller than the average screen size (or about 20 lines). For five-line functions, there is simply no point.

Share this post


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

  • Advertisement