Error On Array Output

Getting Weird Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
using namespace std;

double gpa(char *, char*);

int main()
{
	char arr[256], arr1[256];
	double gpa1 = 0.00;
	cout << "\nEnter : ";
	cin.get(arr, 256);
	cin.ignore();
	cout << "\nEnter 2 : ";
	cin.get(arr1, 256);

	gpa1 = gpa(arr, arr1);
	cout << gpa1 << endl;
	system("pause");
}

double gpa(char *a, char *p)
{
	if (a == "BS(CS)")
	{
		if (p == "1st")
		{
			return 22.40;
		}
	}
}



Output : -1.#IND
Last edited on
Why I cannot start new topic?
if (a == "BS(CS)")
to compare C-strings, use the function strcmp()
 
    if (strcmp(a, "BS(CS)") == 0)
or
 
    if (!strcmp(a, "BS(CS)"))

http://www.cplusplus.com/reference/cstring/strcmp/

Or use C++ std::string instead.


Output : -1.#IND

What value is returned by the function gpa() when the if statement is false?
no value is return when the condition is false
@chervil still getting the -1.#IND output how to fix it
The function promises that it will return a value of type double. If none is specified, that should give a warning message from the compiler, because it means there is a problem in the code. In this case it means some sort of garbage value is returned by default. Instead, you should add some code to specify what you would like the value to be. For example, put return 0.0; or whatever value suits your purpose .

Note also these messages from the compiler:
In function 'double gpa(char*, char*)': 
23:11: warning: comparison with string literal results in unspecified behaviour [-Waddress] 
25:12: warning: comparison with string literal results in unspecified behaviour [-Waddress] 
30:1: warning: control reaches end of non-void function [-Wreturn-type]

Last edited on
thnx error is fix
@chervil i fix my error using strings
have a look
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
double gpa(char *, char *);

int main()
{
	char arr[256], arr1[256];
	double gpa1;
	cout << "\nEnter : ";
	cin.get(arr, 256);
	cin.ignore();
	cout << "\nEnter 2 : ";
	cin.get(arr1, 256);

	gpa1 = gpa(arr, arr1);
	cout << gpa1 << endl;
	system("pause");
}

double gpa(char *a, char *p)
{
	string a1 = a, b1 = p;

	cout << a1 << " " << b1 << " ";


	if (a1=="BS(CS)")
	{
		if (b1=="1st")
		{
			cout << "Pass" << endl;
			return 2.22;
		}
	}
	return 0.0;
}

Yes, that seems to work.

You could simplify it slightly by changing the array parameters to type string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double gpa(string a1, string b1)
{
	cout << a1 << " " << b1 << " ";

	if (a1=="BS(CS)")
	{
		if (b1=="1st")
		{
			cout << "Pass" << endl;
			return 2.22;
		}
	}
	return 0.0;
}

This works even when you pass it a c-string (character array) because it automatically converts the input value to type string when the function is called.
Topic archived. No new replies allowed.