Question:
Help with simple C++ string manipulation program.?
Matthew
2011-02-16 11:57:40 UTC
Here is the code; I'm sorry it is so messy. I need to create a list from a given sentence. Any help is appreciated and I thank you in advance.

#include
#include
using namespace std;

void main()
{
string sentence;
char string1[]="";
char space[]=" ";
char period[]=".";
char word1[]=" ";
char word2[]=" ";

bool x=true;

cout << "This program turns a given sentence into a list of words." << endl;
cout << "Input a sentence: ";
getline(cin, sentence);

cout << endl;

int len = sentence.length();

for(int b=0; b {
string1[b]=sentence[b];
}

int i;
int t;

for(i=0; i {
if(string1[i]==space[0] || string1[i]==period[0])
{
for(t=0; t=i; t++)
{
word1[t]=string1[t];
}
break;
}
}

i=i+2;
int t1=t+2;
int b=0;

for(i; i {
if(string1[i]==space[0] || string1[i]==period[0])
{
for(t1; t1 {
b=0;
word2[b]=string1[t1];
b=b+1;
}
break;
}
}

cout << endl;
cout << "The sentence as a list is: " << endl;

for(int f=0; f=t1; f++)
{
cout << word1[f];
}

cout << endl;

for(int f=0; f=b; f++)
{
cout << word1[b];
}


int endProg=0;

while(endProg==0)
{
cout << "To end the program type 1: ";
cin >> endProg;
cout << endl;
}

}
Four answers:
The Phlebob
2011-02-16 17:45:37 UTC
You never allocate more than two characters (the blank and a terminating null character) for these char arrays:



char word1[]=" ";

char word2[]=" ";





That will cause problems when you try to reference or store into the third character of any of those. You have to allocate enough space to handle the biggest string of characters the array is likely to handle + 1 (for the terminator).



Hope that helps.
?
2011-02-16 12:20:34 UTC
You didn't specify what kind of help was needed.. I'll try to guess



first, compilation errors and how to fix them



test.cc:5:11: error: '::main' must return 'int'

-- change "void main" to "int main"



test.cc:22:35: warning: conversion to 'int' from 'std::string::size_type' may alter its value

-- change the type of len from int to string::size_type or just to size_t (this will also require size_t in the following loop counters)



test.cc:36:36: warning: suggest parentheses around assignment used as truth value

-- change t=i to t == i



test.cc:48:14: warning: statement has no effect

-- in for(i; i


test.cc:52:31: warning: statement has no effect

-- same for for(t1; t1


test.cc:65:24: warning: suggest parentheses around assignment used as truth value

-- change f=t1 to f == t1



test.cc:72:24: warning: suggest parentheses around assignment used as truth value

-- change f=b to f == b



test.cc:14:14: warning: unused variable 'x'

-- get rid of that bool x if it's not used



After that, run-time errors are inevitable because the first loop is going to write past the end of the array string1 on the line string1[b]=sentence[b];, and likewise writing past the end of word1 will happen in word1[t] = string1[t];, and so on.



Consider using actual lists or at the very least vectors, not arrays, for this task, since you cannot know beforehand how many words are going to be in the input sentence.
Erika
2016-10-05 07:00:02 UTC
A evaluate of your search engine optimization metatags shows significant shortcomings through loss of conventional key words and failure to contain a description metatag. except your hotel and eating place is a significant "branded" properly-primary address, you desire to to replace conventional words to describe your corporation, i.e., identify low-value hotel and eating place | Neath, uk
Mirt F
2011-02-16 12:05:26 UTC
1. indent your code

2. give variables meaningful names (not something like "t")

3. comment it!!!



Do these thing and and problems will be much easier to spot for both you and anyone trying to help.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...