I'm a Beginner and there are some problems I do not solve.

HI. I am new in cplusplus. May I ask something basic?
Variable initialization "string" is not working for me.
But int, float, char, and double is working. Only string is not, and I'm using iostream header for it. I'm using Turbo c 7, and I know it is not the latest.
Thanks in advance :)

1
2
3
4
5
6
  #include<iostream.h>
  int n;//this does
  float 1234;//also this
  double 12345;//and this
  char 'c'//and this
  string str = "Hello World"; //but this not applies :( 


Hope you can solve my problems. Thanks :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<string>

  using namespace std;
  
  int n;//this does
  float var1 = 12.34;//also this
  double var2 = 12345;//and this
  char var3 = 'c'; //and this
  string str = "values"; //but this not applies :( 
  
  int main()
  {
    
    system("pause");
    return 0;
      
    }



if you want to use string variable, you have to use std namespace and string library. And I think you should first declare the variable then assign a value to it like in the code above.
Ey. Sir? using namespace std; is not applicable to turbo c 7. it is nothing to it. and sir, may I ask? What is system("pause); for?
This does not work sir :(

Thanks for your reply sir.
Ey. Sir? using namespace std; is not applicable to turbo c 7. it is nothing to it.

Ah, but it does have something to do with the problem. Your ancient compiler is the problem. Perhaps you need to find a compiler from this century so you can use standard C++ features like std::string.

By the way using all those global variables should also be avoided.

system("pause") is to make the console window stay open after execution has finished. All it does is tell you to "Press any key to continue..". Again, it is another bad programming practice.
http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong
Use cin.get() as an alternative, pressing enter to exit.
Last edited on
As Jib says using global variables should be avoided. What he means is that rather than doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<string>

  using namespace std;
  
  int n;//this does
  float var1 = 12.34;//also this
  double var2 = 12345;//and this
  char var3 = 'c'; //and this
  string str = "values"; //but this not applies :( 
  
  int main()
  {
    
    system("pause");
    return 0;
      
    }


You should instead do this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<string>

  using namespace std;
  

  int main()
  {
      int n;//this does
      float var1 = 12.34;//also this
      double var2 = 12345;//and this
      char var3 = 'c'; //and this
      string str = "values"; //but this not applies :( 

      system("pause");
      return 0;
      
  }



That being said, as long as you're not being graded, you can put your variables wherever you please.

Oh yeah. System Pause is totally necessary. If you don't believe me just remove it and watch what happens.
Also like Jib said. Get a better compiler.

You have two options in my opinion

1.) Get a professional IDE like visual studio or NetBeans (They're both free)

OR

2.) Just write your code here: http://cpp.sh/
Last edited on
Oh yeah. System Pause is totally necessary. If you don't believe me just remove it and watch what happens.

Ah, no system("pause") is totally unnecessary and is considered by many to be a very bad practice. (By the way I did remove it and the program ran perfectly!)

Yes that's true. But if you want your program to stay open for longer than a split-second you probably would want to use system pause, no?
cin.ignore(999,'\n')

Enjoy your non system("pause").
Yes that's true. But if you want your program to stay open for longer than a split-second you probably would want to use system pause, no?

No, my system doesn't have a "pause" executable so this system() call is actually ignored. Plus my system doesn't automatically close the terminal window when the program finishes. There are ways to prevent the "window" from closing, in Visual C++ for example you can run the program in a different mode, either run mode or debug mode I don't remember which, or you can run the program from the command line. Using system() for holding the window open is a operating system specific crutch that should be discouraged.
Hehe. Thanks for your answers. But in my class, Turbo c 7 is a must.
We after we excel we will be distributed to other ide's, and compilers.
Good luck, but IMO one can never excel when using outdated tools.

Topic archived. No new replies allowed.