| | |
Help needed with dynamic arrays.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 63
Reputation:
Solved Threads: 0
Hello..
Can someone help me with this tutorial.
only have a background of how to creat a dynamic array in 4 steps but not other than that. And the question seems complicated more than only that. So kindly please someone help me and wxplain it to me.
---------------------
write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence, all string of two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter but should contain no other uppercase letters. Don’t worry about proper names, if there first letter are changed to lowercase, that is acceptable. Treat a line break as if it was a blank, in the sense that line break and any number are blanks are compress to a single blank. Assume that the sentence ends with a period and contains no other period. For example the input
the Answer to life, the universe, and everything
IS 42.
Should produce
The answer to life, the universe, and everything is 42.
Note:
Following functions from might help:
1. str.insert(pos, str2)
2. str.erase(pos, length)
---------------
****Note that I haven't learnt the insert and erase functions yet.
I've no idea what will they do.
Can someone help me with this tutorial.
only have a background of how to creat a dynamic array in 4 steps but not other than that. And the question seems complicated more than only that. So kindly please someone help me and wxplain it to me.
---------------------
write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence, all string of two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter but should contain no other uppercase letters. Don’t worry about proper names, if there first letter are changed to lowercase, that is acceptable. Treat a line break as if it was a blank, in the sense that line break and any number are blanks are compress to a single blank. Assume that the sentence ends with a period and contains no other period. For example the input
the Answer to life, the universe, and everything
IS 42.
Should produce
The answer to life, the universe, and everything is 42.
Note:
Following functions from might help:
1. str.insert(pos, str2)
2. str.erase(pos, length)
---------------
****Note that I haven't learnt the insert and erase functions yet.
I've no idea what will they do.
0
#2 Oct 29th, 2009
Post down your current code. This forum has a rule that we cannot give homework help until you show your effort.
•
•
Join Date: Apr 2009
Posts: 63
Reputation:
Solved Threads: 0
0
#3 Oct 29th, 2009
Ok here is what i've done and not even sure whether these steps are right or not.
C++ Syntax (Toggle Plain Text)
# include <iostream> # include <string> using namespace std; int main() { tepedef string* sentence; sentence a; a= new string[100]; cout<<"Please enter one sentence."; getline(cin,a); delete [] a; return 0; }
•
•
Join Date: Apr 2009
Posts: 63
Reputation:
Solved Threads: 0
0
#4 Oct 29th, 2009
Oh it's typedef up there ! a typo!
but as to this code i'm getting a compiler error that says>>
1>------ Build started: Project: 102 hw, Configuration: Debug Win32 ------
1>Compiling...
1>fofa.cpp
1>c:\users\fofa\documents\visual studio 2005\projects\102 hw\fofa.cpp(15) : error C2664: 'std::getline' : cannot convert parameter 2 from 'sentence' to 'std::basic_string<_Elem,_Traits,_Ax> &'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>Build log was saved at "file://c:\Users\fofa\Documents\Visual Studio 2005\Projects\102 hw\Debug\BuildLog.htm"
1>102 hw - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
but as to this code i'm getting a compiler error that says>>
1>------ Build started: Project: 102 hw, Configuration: Debug Win32 ------
1>Compiling...
1>fofa.cpp
1>c:\users\fofa\documents\visual studio 2005\projects\102 hw\fofa.cpp(15) : error C2664: 'std::getline' : cannot convert parameter 2 from 'sentence' to 'std::basic_string<_Elem,_Traits,_Ax> &'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>Build log was saved at "file://c:\Users\fofa\Documents\Visual Studio 2005\Projects\102 hw\Debug\BuildLog.htm"
1>102 hw - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
0
#5 Oct 29th, 2009
I made some code for this but it is missing a check for the length of input and a newline character remover ( I don't know how you are going to enter a newline character through user input ).
I made 4 functions for this where 3 of them are used in 1 (the main function). See comments.
I hope this helps
I made 4 functions for this where 3 of them are used in 1 (the main function). See comments.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; //makes uppercase to lowercase void makeLower(string &in, int index) { if( in[index] >= 'A' && in[index] <= 'Z' ) in[index] += 'a' - 'A'; } //makes lowercase to uppercase void makeUpper(string &in, int index) { if( in[index] >= 'a' && in[index] <= 'z' ) in[index] += 'A' - 'a'; } //makes 2 or more spaces into 1 void checkSpaces(string &in) { for( int i = 0; i < in.length(); i++ ) if( in[i] == ' ' && in[i+1] == ' ' && i != in.length()-1) { in.erase(i,1); i--; } } //checks the string and does the above functions void check(string &in) { checkSpaces(in); bool peroid = true; for( int i = 0; i < in.length(); i++ ) { if( (in[i] >= 'a' && in[i] <= 'z') || (in[i] >= 'A' && in[i] <= 'Z') ) { if( peroid ) { makeUpper(in, i); peroid = false; } else makeLower(in, i); } if( in[i] == '.' ) peroid = true; } } int main() { /* you can use a char array for getting user input so it limits it to 100 characters and then convert that to a string for checking */ string in; getline(cin, in); check(in); cout << in << endl; system("PAUSE"); //don't think you need this for VC++ but I need it for dev-C++ return 0; }
I hope this helps
![]() |
Similar Threads
- Problem with Dynamic Arrays (C)
- C++ Arrays of unknown size (C++)
- Workaround to (true)dynamic arrays (VB.NET)
- Multyplying one dimensional dynamic arrays(matrix) (C++)
- How to pass dynamic arrays? (C)
- Creating dynamic array structures (C++)
- Creating dynamic arrays (C++)
- Huge Dynamic arrays in QB45, BC7 (Legacy and Other Languages)
- Help w/ dynamic list funtction definitions (C++)
Other Threads in the C++ Forum
- Previous Thread: Viewing .exe File Source / Editing .exe File
- Next Thread: Soccer game c++ PLEASE HELP!!!!!!!
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





