Changing Variable Value Without Input

Hey Guys, This is my first post here at cplusplus.com!
I'm a newbie in C++ so I don't really know that much about the language.
I just wanted to ask if I can change the value of a variable without any user input. Basically I want the variable to be 0 when the script starts and when it gets to a certain point it should be 1.

I know of cin but from what I know it requires user input.

Thanks,
Oskar
The assignment operator (reference below) will do just that! Simply define (or declare) a variable and then use the assignment operator when you feel the need to change the value already being stored in the variable. I have posted an example below.

<identifier> = <value>

http://www.cplusplus.com/doc/tutorial/operators/

Example:
1
2
3
4
5
6
7
8
9
#include <iostream>
 
int main(void) {
    int number = 5;
    std::cout << "Number: " << number << '\n';
    number = 10;
    std::cout << "Number: " << number << '\n';
    return 0;  
}
Last edited on
Thanks, that worked :) but I have a strange problem this is a sample from my code:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 if (song1 == nosong){
if (newmusicwritten == nosong)
ifstream musicfile;  
musicfile.open ((musicindex+musicfileextenstion).c_str());
musicfile << musicnew <<endl;
musicfile.close();
newmusicwritten = "1";}
                      }
                      if (song2 == nosong){
if (newmusicwritten == nosong)
ifstream musicfile;  
musicfile.open ((musicindex+musicfileextenstion).c_str());
musicfile << musicnew <<endl;
musicfile.close();
newmusicwritten = "1";}          
                      }
                      if (song3 == nosong){
if (newmusicwritten == nosong)
ifstream musicfile;  
musicfile.open ((musicindex+musicfileextenstion).c_str()); //This line is the problem
musicfile << musicnew <<endl;
musicfile.close();
newmusicwritten = "1";}         
                      }
                      if (song4 == nosong){
if (newmusicwritten == nosong)
ifstream musicfile;  
musicfile.open ((musicindex+musicfileextenstion).c_str());
musicfile << musicnew <<endl;
musicfile.close();
newmusicwritten = "1";}       
                      }
                      if (song5 == nosong){
if (newmusicwritten == nosong)
ifstream musicfile;  
musicfile.open ((musicindex+musicfileextenstion).c_str());
musicfile << musicnew <<endl;
musicfile.close();
newmusicwritten = "1";}       
                      }

The line that I have marked above is where errors start, the first one is:
219 C:\Users\Oskar\Documents\cPlusPlus\Program\main.cpp 'struct std::string' has no member named 'open'


I am not sure why it gives me the error as it's a copy of the one thats in the "if (song1..." one which gives me no errors.
What are the types of these two variables: musicindex and musicfileextenstion. If you have a compiler that supports the new standard, then there is no need to invoke the member function "c_str()". The new standard has included an overloaded "open" member function that takes a std::string object as an argument (reference below).

EDIT: I've also noticed that you are incorrectly using the std::ifstream object. The operator being used in your code is not overloaded in the std::ifstream class (<<). I believe you are confusing the std::ifstream class with the std::ofstream class (read, write). I have included references to both of these classes below.

http://en.cppreference.com/w/cpp/io/basic_ifstream/open
http://en.cppreference.com/w/cpp/io/basic_ofstream
http://en.cppreference.com/w/cpp/io/basic_ifstream
Last edited on
musicindex and musicfileextenstion (I noticed that I spelled extension wrong :P) are strings the value of musicindex is index and the value of musicfileextenstion is .music . Im using Dev-C++ .
So basically with the new standard it would be something like:
1
2
3
4
5
6
7
8
void open( const std::string index.music,                                  
ios_base::openmode mode = ios_base::in );

musicfile << musicnew <<endl;

musicfile.close();

newmusicwritten = "1";
Topic archived. No new replies allowed.