-2

I have problem with char in my code, please guide me (c++).

I have this error: Run-Time Check Failure #3 - The variable 'op' is being used without being initialized. What does it mean and how do I fix it?

This is my code:

#include  <stdio.h>
#include  <iostream>
#include  <stdlib.h>
#include  <conio.h>
#include  <math.h>

using namespace std;
enum Operations {SIN1, COS1, TAN1};

void selectenteroperation(char *szInput) {
    char *szLabels[3] = {"sin", "cos", "tan"};
    int i=0;
    while(strcmp(szInput,szLabels[i])==0)
        ++i;
    switch (i)
    {
        case SIN1: { cout<<"SIN";   break; }
        case COS1: { cout<<"COS";   break; }
        case TAN1: { cout<<"TAN";   break; }
        default:   { cout<<"Wrong"; break; }
    }
}

void main() {
    char *op;
    cout<<"op?";
    cin>>op;
    if(strcmp(op,"sin")==0) selectenteroperation("sin");
    if(strcmp(op,"cos")==0) selectenteroperation("cos");
    if(strcmp(op,"tan")==0) selectenteroperation("tan");
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Aliexo
  • 61
  • 1
  • 3
  • 8
  • 2
    What is your problem exactly? – GWW Feb 02 '11 at 07:27
  • i have this error : Run-Time Check Failure #3 - The variable 'op' is being used without being initialized. – Aliexo Feb 02 '11 at 07:29
  • 3
    Please post all relevant code in the body of your question. stackoverflow.com questions should be standalone they shouldn't rely on external links. – CB Bailey Feb 02 '11 at 07:30
  • Uh, that compiler error sounds pretty clear to me: You're trying to use the variable `op` before you create/initialize it. Check where you declare the `op` variable, and make sure that you've initialized it before you try to use it! – Cody Gray - on strike Feb 02 '11 at 08:00
  • 1
    @Aliexo: 7 questions now and you never **accepted** any of them (by clicking on the check mark): are you really expecting people to continue to answer when you don't seem to care about thanking those who use their time to help you ? – ereOn Feb 02 '11 at 08:48
  • Moved extra info from comment to question to make it more of a real question. Hopefully will be re-opened but you can never tell. – paxdiablo Feb 03 '11 at 02:51

7 Answers7

5

It's because char *op creates just a character pointer, not the backing storage to hold the string.

Since this is C++, you should be using std::string. Old style C strings have their uses but easy-to-use strings is not one of them.

Embrace C++, there are more than enough C programmers trying to pass themselves off as C++ gurus as it is :-)

Since this looks like homework, I won't give you back your fully fixed program but I will give you one that can be used as a basis for testing and, more importantly, understanding:

pax$ cat qq.cpp ; g++ -o qq qq.cpp
#include <iostream>
int main (void) {
    std::string s;
    std::cout << "Enter something: ";
    std::cin >> s;  // or getline (std::cin, s).
    std::cout << "You entered [" << s << "]" << std::endl;
    return 0;
}

pax$ ./qq
Enter something: hello
You entered [hello]

Alternatively, if you really want to use C strings, something like:

#include <iostream>
int main (void) {
    char s[256];
    std::cout << "Enter something: ";
    std::cin.getline (s, sizeof (s));
    std::cout << "You entered [" << s << "]" << std::endl;
    return 0;
}

may be suitable.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

I think you should check out the standard template library string class instead of using a C-string (char*) for your op variable.

GWW
  • 43,129
  • 11
  • 115
  • 108
2

I'm guessing you probably want to change the line:

while(strcmp(szInput,szLabels[i])==0)

to

while(strcmp(szInput,szLabels[i])!=0)

edit:But you should also have a test here to make sure i doesn't exceed 2.

And of course you also need to allocate memory for *op, like this:

char *op = new char[256] ;

(I just chose 256 arbitrarily here).

UPDATE: To test that i is less than 3:

while(i<3 && strcmp(szInput,szLabels[i])!=0)
Rune Aamodt
  • 2,551
  • 2
  • 23
  • 27
1

You are reading into a uninitalized char* pointer. A quick (if somewhat dangerous fix) would be to write:

 char op[100];

instead of

  char* op;

but using std::strings is much safer as others have pointed out.

Andrew Stein
  • 12,880
  • 5
  • 35
  • 43
0

The answer is that you don't have a char -- you have a pointer to a char. The pointer is uninitialised.

char *op;
cout<<"op?";
cin>>op;

Do something like

char *op = new char[100];

Or use std::string instead.

Kevin A. Naudé
  • 3,992
  • 19
  • 20
0
char *op;
cout>op;

The above code is wrong. Trying to write to some memory that has not been initialized !!

Try using a char array or a string instead of a raw char pointer

Arunmu
  • 6,837
  • 1
  • 24
  • 46
0

Your code has many problems:

  • strcmp returns 0 when the strings are EQUAL, not when they're different
  • you should think a way to stop searching once you get to the end of szLabels
  • string literals cannot be altered and so it's better to use const char * in that case
  • main should not declared that way (it's not a void function)
  • cin >> op is not going to allocate the needed memory for your input
  • C++ has a std::string class that can solve many of the above issues and seems to be just the right thing to use here

I hope you're not going to feel offended, but to me looks like you're using C++ as the first language to learning about programming. I personally think this is a recipe for a disaster.

If for some strange reason you really must use this difficult path then my suggestion is to start from a good introductory book and following examples first.

C++ isn't a good language for just "try and see". IMO typing in code and trying to compile is bad in general, but is a true suicide with a language powerful but complex and asymmetric as it is C++ especially if you consider that when you make a mistake often C++ will just laugh at you instead of clearing telling you where is the mistake.

Community
  • 1
  • 1
6502
  • 112,025
  • 15
  • 165
  • 265