take a single charakter from string

Hello,

I have made a program which reads a string from the user and store it in a vector,
but i have a small problem.

I want something that "reads" the string and if the second position of string is a number i want to store that number in an integer so as to use it..

for example:
string line;
int k;
getline(cin,line);
line[1]==k;

last command is not right but i need something that is able to do it.
string is unknown until user puts it.
**string can be mixed numbers and characters** i just need to take line[1] if only it is a number..
Thanks in advance.
Last edited on
Given the string "a123bcd", the number you want to store is '1' or '123' ?
if i give this string ":2" , i want to take 2 and use it..
if i give this string ":12",i want to take 12 and use it..

OK, you'll need to use stringstreams in that case
http://www.cplusplus.com/reference/iostream/stringstream/
They are like other streams ( like cin/cout/fstream ) but they operate on strings

eg:
1
2
3
4
string line = ":12";
int k;
stringstream ss ( line.substr(1) ); // create the stream from line, removing the first character
ss >> k; // read k from the stringstream 


To check whether the second character of the string is a number you can use isdigit from <cctype>
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/
error :
variable `std::stringstream ss' has initializer but incomplete type

may i have to put some special header except from <iostream> ,<fstream> ??
Yes, <sstream>. See link above for reference
my code is the following :


#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <sstream>


using namespace std;

int main()
{

vector<string> vectorOne;
vector<string> vectorTwo;
string line ;
int i , k ;


for(i=1; ; i++){
cout << i << ">";
getline(cin,line);
vectorOne.push_back(line);
stringstream ss ( line.substr (1) );
ss >> k;

if(line[0]==':'){

if(line[1]=='s'){
vectorOne.erase(vectorOne.end());
for(i=0; i < (long) vectorOne.size(); i++){

cout << i+1 << ">" << vectorOne.at(i) <<endl;
}
}
if(line[1]=='q') break;
if(line[1]=='c'){
vectorOne.clear();
i=0;
}
if(line[1]=='a'){
vectorOne.erase(vectorOne.end());
i=i-1;
}
if(line[1]==k){
i=k-1;
}

}


if(line==":i myout.txt"){
vectorOne.erase(vectorOne.end());
ifstream inputFile("myout.txt");
while(getline(inputFile, line)){
vectorTwo.push_back(line);
}
vectorOne.insert(vectorOne.begin(),vectorTwo.begin(),vectorTwo.end());
i=i-1;
}
if(line==":w myout.txt"){
vectorOne.erase(vectorOne.end());
ofstream outputFile("myout.txt");
for(long index=0; index< (long) vectorOne.size(); index++){
outputFile << vectorOne.at(index) << endl ;
}
outputFile.close();
i=i-1;
}

}
system("PAUSE");
return 0;
}

i want to link 'k' with 'if'.. is it possible??
Please use [code][/code] tags
I don't know what you mean but in if(line[1]==k) you are comparing an int with a character.
If you want to check if the second character is a number ( digit ) use the function I mentioned above
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <sstream>


using namespace std;

int main()
{   
    
    vector<string> vectorOne;
    vector<string> vectorTwo;
    string line ;
    int i , k ;
    
    
    for(i=1; ; i++){
                     cout << i << ">";
                     getline(cin,line);
                     vectorOne.push_back(line);
                     stringstream ss ( line.substr (1) );
                     ss >> k;
                     
                           if(line[0]==':'){ 
                                                
                                          
                                             
                                          if(line[1]=='s'){
                                                           vectorOne.erase(vectorOne.end());
                                                           for(i=0; i < (long) vectorOne.size(); i++){
                                                                                                      
                                                                                                      cout << i+1 << ">" << vectorOne.at(i) <<endl;
                                                                                                      
                                                                                                    }
                                                           }
                                          if(line[1]=='q') break;
                                          if(line[1]=='c'){
                                                           vectorOne.clear();
                                                           i=0;                      
                                                           }
                                          if(line[1]=='a'){
                                                           vectorOne.erase(vectorOne.end());
                                                           i=i-1;                        
                                                           }                
                                          
                                          }
                                                 
                                
                                
                                
                                
                                
                                
                                
                                             
                                 
                                  
                                                    
                     if(line==":i myout.txt"){
                                              vectorOne.erase(vectorOne.end());
                                              ifstream inputFile("myout.txt");
                                              while(getline(inputFile, line)){
                                                                              vectorTwo.push_back(line);
                                                                              }
                                              vectorOne.insert(vectorOne.begin(),vectorTwo.begin(),vectorTwo.end());
                                              i=i-1;
                                              }
                     if(line==":w myout.txt"){
                                              vectorOne.erase(vectorOne.end());
                                              ofstream outputFile("myout.txt");
                                              for(long index=0; index< (long) vectorOne.size(); index++){           
                                                                                                         outputFile << vectorOne.at(index) << endl ;
                                                                                                         }
                                              outputFile.close();
                                              i=i-1;
                                              }

                                                            
                                                                                    
                                                    
                                                    
                                                               
                       
}
    system("PAUSE");
    return 0;
}



if you run this code then it will ask you to enter a string.. I save that string in a vector.

output : 1>
2>
etc
command :s --> shows the context of the vector
command :c --> clears the vector
command :w myout.txt --> saves vector in myout.txt
command :i myout.txt
:s --> shows the index of the .txt file
i want to make the following command :
:12 --> this command will show me line 12 and whatever i write will erase the vector at 12
and repace it with the new line tha i will write.
that's why i want to connect 'if' with 'k'..
Last edited on
Why do you require the input starting with ':' ?
Why are you hard-coding the job that a parser should do?
It's an exercise for my university... if i don't give it then i will fail to lesson...
Topic archived. No new replies allowed.