Which one is the best / or suggest other

/*



Counting in Lojban, an artificial language developed over the last fourty years, is easier than in most languages
The numbers from zero to nine are:
0 no
1 pa
2 re
3 ci
4 vo
5 mk
6 xa
7 ze
8 bi
9 so


Write a program that reads in a lojban string(representing a no less than or equal to 1,000,000) and output it in numbers.
*/


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
#include<iostream>
#include<map>
#include<conio.h>
using namespace std;


int main()
{
    map<char,int> m;
    m.insert(pair<char, int>('n',0));
    m.insert(pair<char, int>('p',1));
    m.insert(pair<char, int>('r',2));
    m.insert(pair<char, int>('c',3));
    m.insert(pair<char, int>('v',4));
    m.insert(pair<char, int>('m',5));
    m.insert(pair<char, int>('x',6));
    m.insert(pair<char, int>('z',7));
    m.insert(pair<char, int>('b',8));
    m.insert(pair<char, int>('s',9));
    
   
    
    char *ch,*ir;
    cout<<"Enter the number in different language\n";
    cin>>ch;
    long long p=0;
    int s=0;
    while(ch[s++]);
    s--;
    ir=ch;
    map<char, int>::iterator pi;
    for(int i=0;i<(s/2);i++)
    {
              pi=m.find(*ir);
              p=p*10 + pi->second;
              ir+=2;
    }
    cout<<p;
    
    getch();
    return 0;
    
    
    
    
}



or


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
#include<iostream>
#include<map>
#include<conio.h>
using namespace std;


int main()
{
    cout<<"enter the string\n";
    char *ch;
    cin>>ch;
    char *ir=ch;
    
    long long s=0;
    while(ch[s++]);
    s--;
    
    long long p=0;
    for(long long i=0;i<(s/2);i++,ir+=2)
    {
             
             switch(*ir)
             {
                        case 'n':
                                 p=p*10+0;
                             break;
                        case 'p':
                                 p=p*10+1;
                             break;
                        case 'r':
                                 p=p*10+2;
                             break;
                        case 'c':
                                 p=p*10+3;
                             break;
                        case 'v':
                                 p=p*10+4;
                             break;
                        case 'm':
                                 p=p*10+5;
                             break;
                        case 'x':
                                 p=p*10+6;
                             break;
                        case 'z':
                        
                             p=p*10+7;
                             break;
                        case 'b':
                        p=p*10+8;
                             break;
                        case 's':
                        p=p*10+9;
                             break;
                        default:
                                cout<<"something different discovered";
                                break;

                        
             }
             
    }
    
    
    
    cout<<endl<<p<<endl;
    getch();
    return 0;
}
Last edited on
They're both broken in different ways. One invariably enters an infinite loop, and the other crashes three lines after entering main().
Well, the first one goes into an infinite loop, printing out "-8446744073709551616" forever...
The second one asks me for a string and freezes...

I'm guessing this wasn't the intent?
Last edited on
Sorry I made a mistake.
I have updated the first code now.

Both are working correctly on DEVc++ on vista platfrom

I want to know about switch statement

When we use switch statement in loop, switch table is created everytime the loop iterates or is it created only once?

You are delivering the user input into a random memory location:

1
2
    char *ch;
    cin>>ch;


What memory does ch point to? You don't allocate any memory for it.

Try this instead:


1
2
    char ch[100];
    cin>>ch;


Ok

You are absolutely right
But it is also working on my computer


Please tell me the answer that I want to know regarding switch statement


But it is also working on my computer
It shouldn't, and even if it is, that's still not excuse. You have to fix it.

When we use switch statement in loop, switch table is created everytime the loop iterates or is it created only once?
If a jump table is created at all, it will be created at compile time, not at run time, so how often the switch is entered makes no difference.
The code using the std::map is probably faster, though (assuming it is correct).

Oh, also,
1
2
//m.insert(pair<char, int>('n',0));
m['n']=0;
It would help if you explained what the program is supposed to do.
Even the "fixed" versions of both programs immediately crash with a segmentation fault after entering a number, for reasons that already have been pointed out.
I have written the problem statement

Sorry , it was my mistake


Can I make the default value as
char * ch='\0';


??
Is it right?

Last edited on
No, of course not.
char* is a pointer to a char, so that makes no sense.
Using std::string woud be the correct approach.
Topic archived. No new replies allowed.