Why doesn't the program work properly?

Hi. İ want to write forexample "24 and 36 gather.13 and 45 gather" and the program must write 60 and 57. But it is gathering 1. Sentence and again gathering 1. Sentence. Why is not it gathering 2. Sentence?

I will further develop the project. so it may be unnecessary parts. ignore them. just explain why I couldn't scan sentence 2. (I wrote this here with google translate)

Sorry for my bad english.

The sources is:
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <cstring>
using namespace std;
int main() {
     char a[900],number[100][50],sentence[100][200];
	 int digit=0,realnumber[100],result,numberofsentences=0,i2=0,i3;
	 int i;//end of the number
	 while(5){
	 i=0;
	 i2=0;
	 cout<< "turkce karakter kullanmadan tamsayilarla islemi giriniz:\n\n";
	 gets(a);
	 while(a[i2]!='.' && a[i2]!=NULL){
		 i3=0;
	 while(a[i2]!='.' && a[i2]!=NULL){
	 sentence[numberofsentences][i3]=a[i2];
	 i2++,i3++;
	 }
	 i2++;
	 if(strstr(sentence[numberofsentences],"ile")!=NULL || strstr( sentence[numberofsentences],"ve")!=NULL){
	 //"ile" and "ve" are and in turkish
	  if(strstr(sentence[numberofsentences],"topla")!=NULL){
	  //topla is gather
	   if(strstr(sentence[numberofsentences],",")==NULL){
	  
		 for(;isdigit(sentence[numberofsentences][i])!=0;i++){
		   number[0][i]=sentence[numberofsentences][i];
		 }
		 realnumber[0]=atol(number[0]);
		 i+=5;
		 for (int axe=0;isdigit(sentence[numberofsentences][i])!=0;i++,axe++){
		 number[1][axe]=sentence[numberofsentences][i];
	   }
	   realnumber[1]=atol(number[1]);
	   result=realnumber[0]+realnumber[1];
	   cout<< endl<<result<<endl;
	   
	  }
	 }
    }
    
numberofsentences++;
}
}
}

https://hizliresim.com/qAE73R
The url is running now.
Last edited on
As a newcomer, please avoid putting links without context in your post, and badly formatted links at that. It looks like it's just a image hosting website, but your formatting makes it look like poor spam. Your image isn't accessible for me anyway.

Please also use code formatting. Edit your code and add [code] and [/code] around your code.

24 + 36 = 60, but 13 + 45 = 58, not 57. Where do you get 57 from?
As it is right now, I don't know what is actually happening in the code, other than you're doing some roundabout way of converting integers to strings or something.

I'll write a C++ program that does what your first sentence wants. It will use strings and C++ streams instead of char arrays. I'm not sure how flexible you want the program, or what restrictions in the sentence syntax you want, so it's hard to tell what is "correct".
Last edited on
Thank you for help.
13+45=57 lol. Im sorry :D
My keyboard error
Last edited on
I just want to make a program that can detect what I'm saying.
Since I can't really make sense of what exactly you want the code to do, I just re-wrote the logic according to your first sentence.

Input
24 and 36 gather.13 and 45 gather
and the output will be 60 and then 58.

You can also do this more than twice, e.g.
24 and 36 gather. 13 and 45 gather. 4 and 5 gather.


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
// Example program
#include <iostream>
#include <string>
#include <sstream>

// input: "24 and 36 gather.13 and 45 gather"
// output: the program must write 60 and 58. 

int main()
{
    using namespace std;
    
    //
    // Get whole sentence as a string
    // 
    
    cout << "Enter text: ";
    string full_text;
    getline(cin, full_text);
    
    //
    // Split string into sentences, using "." character to separate
    //
  
    istringstream sentences(full_text);
    string sentence;
    while (getline(sentences, sentence, '.'))
    {
        //
        // Parse each sentence
        //
        
        istringstream sentence_stream(sentence);
        int num1;
        string conjunction;
        int num2;
        string operation;
        if (!(sentence_stream >> num1 >> conjunction >> num2 >> operation))
        {
            cout << "Error: Bad input\n";   
            return 1;
        }
        if (conjunction != "and")
        {
            cout << "Error: Unsupported token \'" << conjunction << "\'\n";
            return 2;
        }
        if (operation != "gather")
        {
            cout << "Error: Unsupported token \'" << operation << "\'\n";
            return 3;
        }
        
        cout << (num1 + num2) << '\n';
    }
}


Enter text: 24 and 36 gather. 13 and 45 gather.4 and 5 gather.
60
58
9


It first gets the full text from the user, then splits this into sentences using stringstreams w/ getline, and then converts each individual sentence into a stringstream to parse the individual words.

Enter text: 2 and 1 blather.
Error: Unsupported token 'blather'
Last edited on
PS: You should be using fgets instead of gets, if you insist on using C i/o. gets isn't even a valid function in modern C and C++.
http://www.cplusplus.com/reference/cstdio/fgets/
fgets(a, 900, stdin);
Last edited on
Thank you so much.
Topic archived. No new replies allowed.