Warning assistance.....

Here is the program I wrote:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

#include <iostream>  
using namespace std;



void old_string(char[]);
int run_check(char[]);
int change_string(char[]);



char string[40];

int main()//main() function, calls all the necessary functions
{		 
	 char string[40];
	 old_string(string);
	 run_check(string);
	
	

	return 0;
}
	

	void old_string(char string[40])//User enters string input
	{
		cin >> string;
	}

	
	int run_check(char string[])//Here I run my check to make sure the input was legal
	{
		
		int temp=strlen(string);
			bool is_letter=true;
		for(int i=0; i<temp; i++)
		{
			if(!((string[i]>='A'&&string[i]<='Z')||(string[i]>='a'&&string[i]<='z')))
				is_letter=false;
		}

			if(is_letter==false) //If it is illegal input, then program will print "illegal input"
			{					//and return 0.
				cout << "incorrect input\n";
				return 0;
			}

			else if(is_letter==true)//If it is legal input, then the program calls the function change_string().
			{return	change_string(string);}
	}

	int change_string(char string[])//This function creates the required output
	{
		int temp=strlen(string);

		for(int i=0; i<temp; i++)
		{
			
			if(string[i]>=65 && string[i]<=90)
				cout << string[i] << char(string[i]+32);

			else if(string[i]>=97 && string[i]<=122)
				cout << string[i] << char(string[i]-32);
			
		}
	
		cout << endl;
		return 0;
	}



	


The program works perfectly fine (it prints out the necessary output), however, I get these three warnings:

1
2
3
4
5
6
7
8
9
10

1>c:\documents and settings\root\my documents\visual studio 2005\projects\ex7a\ex7a\ex7a.cpp(35) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data


1>c:\documents and settings\root\my documents\visual studio 2005\projects\ex7a\ex7a\ex7a.cpp(55) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data


1>c:\documents and settings\root\my documents\visual studio 2005\projects\ex7a\ex7a\ex7a.cpp(51) : warning C4715: 'run_check' : not all control paths return a value



Are these warnings critical? I don't really understand what they mean. Are they just theoretical warnings, or warnings of actual potential input that could cause errors?

Thanks to whoever answers.
You can ignore "possible loss of data" warnings if you know what you're doing.
As for C4715, You should always make sure that a function that says it's going to return a value always returns a value. Recheck run_check() and you'll see that there's one case in which it doesn't return anything.
On the first warnings: You are converting a size_t (an unsigned type, probably more than 32 bit) to an int (a signed type, probably 32 bit). In this process, there is a risk of data loss. If the vaule held in the size_t is greater than 2^31, then this value will not be ciorrectly represented in the integer type.

As helios says, this is not a problem if you know what you're doing, i.e., if you are positive that the size_t will never hold such big values. But then again...you should correct your program so that no warnings are issued on compilation. Warnings are there for a reason...
I checked its only

c:\documents and settings\usman\desktop\a__.cpp(51) : warning C4715: 'run_check' : not all control paths return a value


so you need not to worry , only simple conditions can control if you want to remove , else its fine ... No Problem
@helios, Corpus & Grey: You guys haven't really addressed the other problems in this code.

@nayeret43: This code has a few issues with it.
Line 13 & 17: Your declaring 2 variables with the same name. Both are character arrays too. Why not use the C++ string type?

Line 29: Your using cin >> to take input into a character array. You have no bounds checking or anything. I highly suggest you read:
http://www.cplusplus.com/forum/articles/6046/ to learn how to take string input correctly.

Lines X: You've called a variable "string". In C++ string is a known type, and your going to cause problems with this later on. You should select a better name for your character arrays like "buffer" or "charArray" as they are not technically strings.

Line 36: This may not be an issue, but I cannot recall if cin >> will null terminate your char array. If not, then the strlen() function is going to return an incorrect value because you haven't init your array.

Line 40: You should be able to use the isAlpha() function to do this instead.

Line 41: You don't need to set a flag here, you should just cout the error and return. This will save execution time and code.

Line 50: This line isn't needed, as if the variable is not false it's implicitly true and will execute. This line also causes an error because you've wrapped the return and made the compiler think that it may not find a valid return statement.

Line 54: There is actually a function called toUpper() and toLower() that will make a character uppercase or lowercase.

e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

int main() {

  string input = "";
  cout << "Enter a string: ";
  getline(cin, input);
  cout << "You have entered: " << input << endl;

  for (unsigned i = 0; i < input.length(); ++i)
    input[i] = toupper(input[i]);
  cout << "Uppercased: " << input << endl;

  for (unsigned i = 0; i < input.length(); ++i)
    input[i] = tolower(input[i]);
  cout << "Lowercased: " << input << endl;

  return 0;


You need to become more familiar with the data types and functions that are provided within the language. This will save you writing excess code to do what can already be done.
hmm, if I remember correctly toupper and tolower are included in ctype.h making them C functions?

I'm not sure but I think the technically correct way to do it in c++ is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

int main() {

  string input = "";
  cout << "Enter a string: ";
  getline(cin, input);
  cout << "You have entered: " << input << endl;

  transform(input.begin(), input.end(), input.begin(), ::toupper);
  cout << "uppercase : " << input << endl;
  transform(input.begin(), input.end(), input.begin(), ::tolower);
  cout << "lowercase : " << input << endl;

  return 0;
}
Last edited on
The first 2 warnings:

the return value of strlen() is unsigned and you could lose data, when storing it into an signed integer. You don't need the function, when you change your for-loop to
for(int i=0;string[i]!='/0';i++)

The last warning:

you should generally think about your usage of return values. The function run_check return 0 if there is incorrect input and returns 0 if not (because change_string always returns 0, too). This doesn't make sense. change_string could also be void and run_check should not return 0, when an error occurred.
Topic archived. No new replies allowed.