hi!!!! i'd like to ask if anyone knows how could i possibly use a string as a number...i mean:
i've got something like cin>>word;
but if that word is a number(like 1,2,3... etc) i have to use that number for going in that line.
if user types ''hello'' its ok.but if he types ''4'' i heve to use that number for going at ARRAY[4].
HOOOOOOOOOOOOOW????
(I am using a vector in which the user types words.if he types a number i have to go at that line of vector and begin wgiting all over again from line 4[example])

First step is to check if the first character is a numeric digit or an alpha character. If its a digit then just subtract '0' to make it binary

char input[]= "4";
int x = input[0] - '0';

Or you can use atol() to convert a string of digits to binary

char input[] = "123";
int x = atol(input);

Many progrmmers prefer strtol() instead of atol() because it catches errors better.

how can i use thai inside an IF? i have to check if the word that the user entered is a number and after that,to get at the vector's line-number. if the word is 4 i have to go an the forth line of the vector and begin writing all over again the vector below the third line.

i have to make a prog that takes words from the user (cin>>word).
i have also a counter that points in which line the user is
(example)
1>hello
2>word
3>how are u?
4>i want to go to line 2 and begin all over again.so line 2 is going to be replaced just like 3,4,5...
5>:2
2>_

this is what i mean..so how can i use the word(string) so that i can make counter=word when word is number?sorry if i confused u.. :/ i think that i have to put all these in an IF!
if (word is number)
{counter=word,delete everything else below line 2 in my vector;}

It will not be a simple thing to do.

std::string word = "i want to go to line 2 and begin all over again.so line 2 is going to be replaced just like 3,4,5";
// search word for the first occurence of a digit
for(int i = 0; i < word.length(); i++)
{
   if( isdigit(word[i]) )
   {
        // found it, so do something with it
   }
}
#include <string>
#include <iostream>
#include <sstream>

int to_number( const std::string& word )
{
    std::istringstream stm(word) ;
    int n ;
    if( ( stm >> n ) && stm.eof() ) return n ;
    else throw 0 ;
}

int main()
{
    std::string word ;
    int counter = 1 ;
    while( std::cout << counter << "> " && std::cin >> word )
    {
        try { counter = to_number(word) ; }
        catch(int) { ++counter ; }
    }
}

WELL THIS IS MY CODE!IT'S A WORD EDITOR(AT LEAST IT TRIES TO BECOME ONE)

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    vector<string> eisagwgeas;
    string word;
    int num;
    unsigned int counter = 1;
    cout<<counter<<">";
    while(getline(cin,word)&&word!=":q")
   {                        
     my_vector.push_back(word);
     if(int num = atoi( word.c_str() ))
     {counter=num;
      my_vector...}[B][I][U]// HOW DO I GO AT MY VECTOR'S NUME LINE?? IF NUM=2 I WANT TO GO AT THE SECOND LINE AND REPLACE WHATEVER IS THERE WITH NEW WORDS THAT I WILL WRITE FROM THE CONSOLE..
[/U][/I][/B]
     if (word==":c" || word==":s")
      {int u=0;}
      else
      {cout<<++counter<<">";}         
     if (word==":s")
     {              
      my_vector.pop_back();              
      int ii;
      for(ii=0; ii < my_vector.size(); ii++)
       {
        cout << my_vector[ii] << endl;
       }
       cout<<counter<<">"; 
     }
     if (word==":c")
        {
              my_vector.clear();
              counter=1;
              cout<<counter<<">";
        }
   }
}

Edited 6 Years Ago by WaltP: Added CODE Tags... AND STOP YELLING AT US!

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    vector<string> eisagwgeas;
    string word;
    int num;
    unsigned int counter = 1;
    cout<<counter<<">";
    while(getline(cin,word)&&word!=":q")
   {                        
     my_vector.push_back(word);
     if(int num = atoi( word.c_str() ))
     {counter=num;
      my_vector...}/[B]/ HOW DO I GO AT MY VECTOR'S NUME LINE?? IF NUM=2 I WANT TO GO AT THE SECOND LINE AND REPLACE WHATEVER IS THERE WITH NEW WORDS THAT I WILL WRITE FROM THE CONSOLE..[/B]

     if (word==":c" || word==":s")
      {int u=0;}
      else
      {cout<<++counter<<">";}         
     if (word==":s")
     {              
      my_vector.pop_back();              
      int ii;
      for(ii=0; ii < my_vector.size(); ii++)
       {
        cout << my_vector[ii] << endl;
       }
       cout<<counter<<">"; 
     }
     if (word==":c")
        {
              my_vector.clear();
              counter=1;
              cout<<counter<<">";
        }
   }
}

Edited 6 Years Ago by WaltP: Added CODE Tags -- again. Use them!

This article has been dead for over six months. Start a new discussion instead.