1

For integer input you can do this.

for (int i = 0; i < count; i++) {
    scanf("%d,", &storage[i]);
}

Is there a way to read string input separated by comma too? example. string1, string2, string3

Below doesn't seem to work or am i missing something?

for (int i = 0; i < count; i++) {
    scanf("%s,", storage[i]);
}

We haven't touch using strtok yet, so I might be questioned why I use it. It is working on integer inputs so I am wondering if it is also possible for string inputs automatically separating them. So I guess I have to read the string inputs 1 by 1 then.

fyeth fyeth
  • 93
  • 3
  • 10
  • Possible duplicate of [Using strtok in c](http://stackoverflow.com/questions/8106765/using-strtok-in-c) – Kishore Nov 23 '15 at 05:44
  • read the whole line into a buffer, then copy the strings up to the commas. e.g. fgets(...) – AndersK Nov 23 '15 at 05:45
  • thanks, actually I can read it as a whole string and use strtok to separate the words using fgets, but we haven't touch yet using strtok. So if it is not possible I guess I have to read them 1 by 1. – fyeth fyeth Nov 23 '15 at 05:50
  • 1
    @fyethfyeth You can just use `scanf` for this , just specifiers should be taken care of . – ameyCU Nov 23 '15 at 05:57
  • I am still of the opinion that the optimal way to process a comma separate file is by using the *pointer* and *end-pointer* required by `strtol` (etc, the `strtoX` family of functions). A simple loop of `while (errno == 0)` enclosing the call to `strtol (p, &ep, base)` and then setting `p = ep;` (or (`p = (ep + 1)` ) is basically all that is required. – David C. Rankin Nov 23 '15 at 06:04
  • @DavidC.Rankin IMHO , there is no need to make things complicated for OP , he maybe a beginner :-) – ameyCU Nov 23 '15 at 06:07
  • Probably right, but sure does eliminate all the *round format peg in square format-specifier hole* problems. – David C. Rankin Nov 23 '15 at 06:12
  • @ameyCU you got it partly right :) well requirements is to use only stuffs that we had learned so far (although I know how to use strtok, strtol and other builtin libraries). – fyeth fyeth Nov 23 '15 at 06:12
  • Ok, the only thing you have to be real careful with using the `scanf` *format-specifier* is that it will actually read all values (including the last one without a comma that follows it). Doable, but it is what makes `scanf` rather inflexible when reading multiple values from a file. Please post a few lines of your input, so we will have a better idea what it is you need. – David C. Rankin Nov 23 '15 at 06:14

2 Answers2

2

%s eats your separator(,) too.

To avoid this you can use scan set.

scanf(" %[^,\n],", word);

The space before the scan set is to avoid any white space before the word. your word may be end with newline or a comma.

EDIT:

Any character between %[^ and ] will denote end of cstring. Here, we got comma(,) and newline (\n). So, the %[^,\n] read our desired string. Now, the input may contain a , after it. the , next to %[^,\n] discards it from the input like it did in your example about integer.

Shakil Ahamed
  • 587
  • 3
  • 18
0
#include<iostream>
#include<math.h>
using namespace std;
int getnum(string s,int j,int k)
{
    int len=s.length();
    int l=j-k;
    int num=0;
    for(int i=k;i<=j;i++)
    {
    if(s[i]==',' )
    {
        num+=(s[i]-'0')*pow(10,l);
    }
    else
    {
        num+=(s[i]-'0')*pow(10,l);
        l--;
    }
    }

return num;
}

int main()
{
    int n;
    cin>>n;
    int a[10];
    int m=0;
    string s;
    cin>>s;
    int len=s.length();
    static int f=0,j=0,i,k=0;
    int num[n];
    for(i=0;i<=len;i++)
    {


    if(s[i]==','|| i==len)
    {
        if(k==0)
        {
            num[f]=getnum(s,j-1,k);
        }
        else
        {
            num[f]=getnum(s,j-1,k);
        }
        f++;
        k=i+1;
        j=i+1;
        continue;
    }
    else
    {
        j++;
        continue;
    }
    }

for(int i=0;i<f;i++)
{
    cout<<num[i]<<endl;
}
    return 0;
}
Sarathi Shah
  • 305
  • 1
  • 2
  • 8