Strings and characters... bane of life...

#include <iostream>
#include <string>
using namespace std;
int main(){
int numberofchars;
string TempString;
bool moveon;
//variables

while(moveon=false){
cout << "how many characters do you want to input?" << endl;
cin >> numberofchars;
char InputString[numberofchars];
cin >> TempString;
if(TempString.length()!=strlen(InputString)){
cout << "strings not equal" << endl;
moveon=false;
}
else moveon=true;
}
cin.getline(InputString,numberofchars, '/n');
cout << InputString;
}

The error is 'InputString' undeclared (first use this function) on line 23.
WHY?

im thinking there might be something wrong with my compiler... sometimes when i hit compile and run it only compiles and doesnt run it. I have to manually go to the .exe file and run it from there :S
im using dev-c++ at the moment... what does everyone else use?

Thank you for reading and any responses are greatly appreciated :)

As you declared InputString inside the while loop, it cannot be seen outside of 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
#include <iostream>
#include <string>
using namespace std;
int main(){
    int numberofchars;
    string TempString;
    bool moveon;
    //variables

    while(moveon=false){
        cout << "how many characters do you want to input?" << endl;
        cin >> numberofchars;
        char InputString[numberofchars];
        cin >> TempString;
        if(TempString.length()!=strlen(InputString)){
            cout << "strings not equal" << endl;
            moveon=false;
        }
        else
            moveon=true;
    } // InputString NO LONGER VISIBLE AFTER THIS SCOPE CLOSES!

    cin.getline(InputString,numberofchars, '/n'); // no such variable!
    cout << InputString;
}


Andy

P.S. See "How to use tags" -- to make your code easier for people to read!
http://www.cplusplus.com/articles/z13hAqkS/

P.P.S. As InputString is full of random stuff (as it's un-init) on line 15 -- when you check it's length -- your test is going to do weird things. Maybe even crash!
Last edited on
Topic archived. No new replies allowed.