0

For my class, I am to write a program in C++ that converts each character in a sentence to the opposite case (upper to lower, lower to upper). We are supposed to use arrays and a user-defined method, and this is what I came up with:

#include <iostream>
using namespace std;

// declare variables
int count = 0;      // array counter
int i = 0;          // loop control
char ch[100];     // each character entered will be stored in this array
char newCh[100];  // this will hold each character after its case has        been changed


main()
{
    cout << "Enter a sentence." << endl;   // prompts user

    while ( ch[count] != '\n' )            // loop continues until "enter" is pressed
    {
        cin >> ch[count];                  // store each character in an array
        count += 1;                        // increment counter
    }
    int convert();                         // call user-defined function
}

// even though it isn't necessary, we are using a user-defined function to perform the conversion
int convert()
{
    for ( i = 0; i >= 0; i++ )
    {
        if ( (ch[i] > 64) and (ch[i] < 91)
        )
        {
            newCh[i] = tolower(ch[i]);
        }
        else
        {
            newCh[i] = toupper(ch[i]);
        }
        cout << newCh[i];
    }
}

I'm not sure why, but it doesn't work. I don't believe that my while loop is terminating and executing the rest of the program. Any advice would be greatly appreciated.

  • 2
    `for ( i = 0; i >= 0; i++ )` This looks very wrong! – drescherjm Mar 03 '15 at 23:14
  • In main `int convert();` is a declaration. Meaning you are never calling convert. Remove the `int` – drescherjm Mar 03 '15 at 23:16
  • Also [this](http://stackoverflow.com/questions/21805674/do-i-need-to-cast-to-unsigned-char-before-calling-toupper). – Baum mit Augen Mar 03 '15 at 23:16
  • I was just taking a guess, nothing else I had was working, so I started testing random conditions to see if ANYTHING happened, but I haven't gotten this to work properly. – Logan Moriarty Mar 03 '15 at 23:17
  • 1
    I recommend you spend 10 minutes and learn the basics of your debugger. – drescherjm Mar 03 '15 at 23:18
  • @LoganMoriarty That is an... interesting approach to say the least. 0.o – Baum mit Augen Mar 03 '15 at 23:18
  • Don't compare characters to numbers. Compare numbers to numbers and characters to characters. For example: `if ( (ch[i] >= 'A') && (ch[i] <= 'Z')`. Don't compare characters to numbers, as nobody will know the character encoding you are using. The character constants are more readable and less error prone. – Thomas Matthews Mar 04 '15 at 00:20
  • You are not checking for overflows. Your program can store over 100 characters in the character array because you don't check boundary conditions. – Thomas Matthews Mar 04 '15 at 00:22

3 Answers3

0

The loop condition in while ( ch[count] != '\n' ) is wrong, as all entries in ch will be initialized to zero by the compiler, and as you increase count inside the loop the condition will never be false and you have an infinite loop, causing you to write beyond the limits of the array.

And writing beyond the limits of an array leads to undefined behavior, and will cause your whole program to be illegal.

I suggest you learn about std::string and std::getline.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

There's a problem with your for loop - you want for ( i = 0; i < count; i++ ). Also your function can be void and you need to pass the count value into it (and you just need to invoke it with convert() without int or void in front.

sinewave440hz
  • 1,265
  • 9
  • 22
0

I have rewrite your code with some modification. The following code works perfectly in my machine -

#include <iostream>
#include<cstdio>
#include<string>
using namespace std;

void convert(char *, int);

string line;
char input[1024];
char output[1024];

main()
{
    cout << "Enter a sentence." << endl;


    while (getline(cin, line)) { // POINT 1
        cout<< line<<endl;

        //converting to char array since you need char array
        //POINT 2
        for(int i=0; i< line.length(); i++){
            input[i]=line[i];
        }

        convert(input, line.length());
        cout<<output<<endl;
        input[1024] = {0};  //POINT 3
        output[1024] = {0};
    }

}

//Custom Convert Method
void convert(char input[], int size){

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

        if(input[i] >= 'a' && input[i] <= 'z'){
            output[i] = toupper(input[i]);
        } else {
            output[i] = tolower(input[i]);
        }

    }
}

Note some points (in my comment) here -
POINT 1: reading a n entire line using getline() method. Here line is a string

POINT 2: since you need char array here I am converting the string line to char array input[1024]

POINT 3: input and output array are being reset to work with the next value;

Output of the code:

enter image description here

"Ctrl+C" will terminate the program

Hope it will help you.
Thanks a lot.

Razib
  • 10,965
  • 11
  • 53
  • 80
  • Wow, thanks a lot. I didn't go about it this way, but I was able to make my program work. This did, however, possible help me for a future assignment, I can definitely use some of the things you used for it. – Logan Moriarty Mar 04 '15 at 01:16