Errors because of compiler

My code is working fine when I compile with dev-c++, but when I use Windows Visual C++ it comes up with the following error messages. The problem is I don't know what my professor uses. Any advice?

1>c:\users\jacob\documents\cis\prgm3.cpp(10) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
1>c:\users\jacob\documents\cis\prgm3.cpp(11) : error C2057: expected constant expression
1>c:\users\jacob\documents\cis\prgm3.cpp(11) : error C2466: cannot allocate an array of constant size 0
1>c:\users\jacob\documents\cis\prgm3.cpp(11) : error C2133: 'stringAsArray' : unknown size

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 str;
    cin>>str;
    int arraySize, i;
    arraySize=str.size();
    char stringAsArray[arraySize];
    for(i=0;i<arraySize;i++)
    {
         stringAsArray[i]=str.at(i);
         stringAsArray[i]=toupper(stringAsArray[i]);
    }
    string output(stringAsArray, arraySize);
    cout<<output<<endl;
    system("pause");
    return 0;             
}
I don't know why dev allows this, but it's not correct programming. And you should update to wxdev.
First of all, you can't just define an array from a variable. That's the problem with "expected constant expression". The solution is to allocate dynamically. If you don't know how, read the tutorial on this website; it has a section on dynamic allocation.
The next thing is the same thing, really. It has to prepare for the contingency that you have an empty string, since you've allocated the array statically - its size is determined at compile time. Since the string might be empty, the size might be zero. That is not allowed. The last one is also the same - you need to know the size. It must be constant to allocate an array statically.
It is a GCC extension, provided courtesy of C99, which permits variably-sized arrays.

The warning: you cannot have a negative array size (or string size) so use an unsigned int.

On system("pause"): http://www.cplusplus.com/forum/articles/7312/

Hope this helps.
I read the dynamic memory section, although I don't understand it completely I think my code is supposed to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    int i;
    cin>>str;
    int arraySize=str.size();
    char * stringAsArray;
    stringAsArray = new char [5];
    for(i=0;i<arraySize;i++)
    {
         stringAsArray[i]=str.at(i);
         stringAsArray[i]=toupper(stringAsArray[i]);
    }
    string output(stringAsArray, arraySize);
    cout<<output<<endl;
    system("pause");
    return 0;             
}

The problem is I'm now getting the errors:
1>ifstatement.obj : error LNK2005: _main already defined in array.obj
1>prgm3.obj : error LNK2005: _main already defined in array.obj
1>Debug\prgm3.exe : fatal error LNK1169: one or more multiply defined symbols found

It still works fine in dev-c++ though.
closed account (z05DSL3A)
You have two files in your VC project and both have a main() defined.
Yeah Grey Wolf!

Also, line 12:
stringAsArray = new char [arraySize];

You should also free dynamic memory when you are done with it.

18
19
20
   string output(stringAsArray, arraySize);
   delete [] stringAsArray;
   cout << output << endl;
closed account (z05DSL3A)
Duoas, I don't seem to be able to stay away, so I may as well start posting again. ;0)
Topic archived. No new replies allowed.