473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C++ strtok

eboyjr14
Let's say that:

argv[i] = "Db4+2";

How can I get the number 2 from that as an integer?

In PHP it would be easy:

[PHP]
<?php
$argv[$i] = "Db4+2";

$argvarr = explode("+", $argv[$i]);
echo $argvarr[1];
// Would return: 2

?>
[/PHP]

I know PHP fluently, and I am just learning C++.
Jun 23 '07 #1
4 1868
Remeber there may not be a +2 or whatever, it could just be Db4
Jun 23 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Maybe something like:
Expand|Select|Wrap|Line Numbers
  1. int length = strlen(argv[i]);
  2. int value = 0;
  3. for (int ctr = 0; ctr < length; ++ctr)
  4. {
  5.    if (argv[i][ctr] == '2')
  6.    {
  7.        value = argv[i][ctr] - 48;
  8.     }
  9. }
  10. cout << value << endl;
  11.  
This is just a loop that searchs for the character 2 and if found changes it to an integer 2 by subtracting the offset from '2' to 2 in the ASCII table.
Jun 23 '07 #3
Maybe something like:
Expand|Select|Wrap|Line Numbers
  1. int length = strlen(argv[i]);
  2. int value = 0;
  3. for (int ctr = 0; ctr < length; ++ctr)
  4. {
  5.    if (argv[i][ctr] == '2')
  6.    {
  7.        value = argv[i][ctr] - 48;
  8.     }
  9. }
  10. cout << value << endl;
  11.  
This is just a loop that searchs for the character 2 and if found changes it to an integer 2 by subtracting the offset from '2' to 2 in the ASCII table.

but... it could be any number, not 2. But... I already figured it out. I would show you the source but its on my iPod and I don't have it with me. Thx, anyway!
Jun 25 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
but... it could be any number, not 2. But... I already figured it out.
I presume you know how to use the ASCII table. Yes?
Jun 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ram Laxman | last post by:
Hi all, I have written the following code: /* strtok example */ #include <stdio.h> #include <string.h> static const char * const resultFileName = "param.txt";
13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
3
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
29
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.