Program just skips to the end.....

I wrote this program, which is supposed to have the user put input in (maximum 40 characters), and if it contains a character which is not a letter, the program will print out "incorrect input", and if they are all letters, then the program will print out the original string, only with after every letter, it will print out it's opposite size equivalent.

An example: Input: tyhSg
Output: tTyYhHSsgG

When I try to run the program it just goes to the end, with "Press any key to continue . . .", and there is no input and no output.

I know that my program is not very well written at all. I am a beginner, so I really need somebody to explain step by step what I am doing wrong.

Thanks to all who answer.

Here is the program:


#include <iostream>
using namespace std;



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


char string[40];

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


return 0;
}


void old_string(char string[40])
{
cin >> string;
}


int run_check(char string[])
{
bool is_letter=true;

int temp=strlen(string);
for(int i=0; i<temp; i++)
{
if(!((string[i]>=65&&string[i]<=90)||string[i]>=97&&string[i]<=122))
is_letter=false;
}

if(is_letter==false)
{
cout << "incorrect input\n";
return 0;
}

else if(is_letter==true)
return change_string(string);
}

int change_string(char string[])
{
int temp=strlen(string);

for(int i=0; i<(temp); i++)
{

if(string[i]>=65 && string[i]<=90)
cout << string << char(string+32);

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


Again, thanks to all who answer.
In main you are not calling the functions, you are declaring them.
You have to fix it in this way:
1
2
3
4
5
6
7
8
int main()
{
    char mystring[40];
    old_string(mystring);
    run_check();
    change_string();
    return 0;
}

When posting code, use [code][/code] tags
Last edited on
Thank you very much. I fixed that. But now there is a different problem. The program prints out very strange output.

Here is the program itself fixed from the last version:

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

#include <iostream>  
using namespace std;



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


char string[40];

int main()
{
	 char string[40];
	 old_string(string);
	 run_check(string);
	 change_string(string);
	

	return 0;
}
	

	void old_string(char string[40])
	{
		cin >> string;
	}

	
	int run_check(char string[])
	{
		bool is_letter=true;

			int temp=strlen(string);
		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)
			{
				cout << "incorrect input\n";
				return 0;
			}

			else if(is_letter==true)
			{return	change_string(string);}
	}

	int change_string(char string[])
	{
		int temp=strlen(string);

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

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



Now, let's say I put in hello

This is what the program will print out:
1
2
3
 
hello↑hello↑hello↑hello↑hello↑hello↑hello↑hello↑hello↑hello↑Press any key to con
tinue . . .



There are basically two problems here.

Number one: The program is supposed to just print out the actual word like this hHeElLlLoO

Number two: How did these signs ↑↑↑↑ come in?

Comment: I think that the problem may be in these lines:
1
2
3
4
5
6
7
8
9
10
 
for(int i=0; i<(temp); i++)
{

if(string[i]>=65 && string[i]<=90)
cout << string << char(string+32);

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


I cannot really locate the exact problem, but I think it might be there. If it is, how do I fix that? (I basically tried to print out the letter and it's opposite size equivalent-just like I explained in the beginning of the program).

Thanks!
That is char(string-32), If you want to output the string in uppercase use the toupper() function (notice that it works with only 1 character per time)
Is there possibly another way to do this without using that function? Something more in the direction to what I was doing?

Thanks!
Can someone please assist me?
You should do your operations for each character in the string.
So from your previous code you have to modify char(string+32); to char(string[i]+32);
I thought I would make the program, comment it well & hope you enjoy it. =)

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
#include <stdio.h> // load the C standard input output library

bool is_alpha(char str[]) // create a Boolean function is_alpha to take a string (str) & return if it is alpha (ie, only letters a-z / A-Z)
{
  for(unsigned int c=0; str[c]!='\0'; c++) // start a loop, counter of "c" from 0 to the end of the string '\0' (end of string)
  {
	if(!( (str[c]>='a'&&str[c]<='z') || (str[c]>='A'&&str[c]<='Z') )) // if the character is NOT a-z OR A-Z
    {
	  return false; // then it is not alpha so return false
    }
  }
  return true; // if the function reaches here - the string is alpha
}

void do_opposite(char str[]) // create a subroutine to do the weird opposite cased character thing needed...
{
  for(unsigned int c=0; str[c]!='\0'; c++) // start a loop, counter of "c" from 0 to the end of the string '\0' (end of string)
  {
	if(str[c]>='a'&&str[c]<='z') // if the character is a-z, then print the character & then the upper-case version of it
	{
	  printf("%c%c",str[c],str[c]-32);
	}
	else // otherwise (the character is A-Z), print the character & then the lower-case version of it
	{
	  printf("%c%c",str[c],str[c]+32);
	}
  }
}

int main() // create the main function
{
  char str[40]; // create a string of length 40
  printf("Enter string: "); // tell the user to enter a string
  gets(str); // get the keyboard input
  if(is_alpha(str)) // if the string is alpha (only characters)
  {
	do_opposite(str); // then print the opposite stuff needed
  }
  else // otherwise
  {
	printf("String is not alpha (only characters)\n"); // tell the user they entered an invalid string
  }
  getchar(); // let them see the input, before they press enter to terminate the program
  return 0; // end the main function with no errors
}
Last edited on
Thank you both very much. My program is working now. :)
No problems, any time.
Topic archived. No new replies allowed.