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)
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".
// 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()
{
usingnamespace 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'
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);