In one of programs I have, sentence.at(0) cout first character, in the other doesn't. Confused at first place that .at() can be applied at string at all. Thank you in advance for your help.
// it works - string.at(0) for first character
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string sentence;
getline(cin, sentence);
for (int i = 0; i < sentence.length(); i++)
{
sentence.at(i) = toupper(sentence.at(i));
cout << sentence.at(i);
}
return 0;
}
///here works - string.at(1) for first character
#include <iostream>
#include <string>
usingnamespace std;
int main(void)
{
int n;
cin >> n;
string text;
cin >> text;
//input this: H*YAECTANENRHMPEADRRAPWR*YISY**M*S*
int m;
m = text.length()/n;
char mytable[m][n + 1];
for(int i = 0; i < m; i++)
mytable[i][n]='\0';
int k(0); // int k = 0;
for(int j = 0; j < n; j++)
for(int i = 0; i < m; i++)
mytable[i][j] = text.at(k++); //text.at(1) in first step
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
if(mytable[i][j]!='*')
cout << mytable[i][j];
else
cout<<" ";
return 0;
}
use [] instead of at most of the time.
sentence[i] = toupper(sentence[i]);
You use of at() seems correct; I would validate the LOGIC around line 44. Print to the screen in that double loop what you are loading to mytable... something must be wrong in there.
Don't expect a post-fix increment to actually be evaluated AFTER the variable use in a function call (it can be undefined behavior until C++17):
1 2 3 4 5 6 7 8 9 10
int k(0);
for (int j = 0; j < n; j++)
{
for (int i = 0; i < m; i++)
{
mytable[i][j] = text.at(k);
k++;
}
}
Recommend always using braces for statement blocks, makes it easier to add statements without messing up the logic.
What is your compiler? A modern C++ compiler won't allow variable length arrays (VLAs) as you did with your 2D char array at line 38. The size parameters have to be compile-time constants.
If you need a run-time container you should use a std::vector. Declaring a sized 2D vector could look something like this: std::vector<std::vector<char>> mytable(m, std::vector<char>(n + 1, '\0'));
This creates your 2D container and automatically fills the elements with the null character, you can delete/comment out lines 40-41.
A modern C++ compiler won't allow variable length arrays (VLAs)
modern/current g++ supports them if not instructed to complain explicitly. Actually g++ supports a great many things unless told not to, its quite forgiving.
I'm using cpp.sh as well, but it has no debugger. As far as I see, nor Coliru have debugger. I'm not so good in debugging, but can manage elementary staff. Actually, I combine compilers and when one of them freeze, just jump to another. Got installed the code::blocks as well... and thinking about dev cpp also.
modern/current g++ supports them if not instructed to complain explicitly. Actually g++ supports a great many things unless told not to, its quite forgiving.
When properly configured g++ will only support standard C++ and will be quite pedantic. However if you fail to properly configure your compiler (any compiler) you will probably be able to use quite a few compiler specific hacks to aid in your self destruction.
Got installed the code::blocks as well... and thinking about dev cpp also.
If you are using Windows consider Visual Studio 2017. It is free in the Community edition and has a decent built-in debugger.
It takes a while to download, and Visual C++ is not installed as part of the default setup.
Orwell's Dev-C++ was outdated when it was last released back in April 2015, the compiler was not the latest then.
When I installed VS2017 I created an offline install instead of using the web install. It required a bit of HD space, but was worth the time and effort to have the offline install.
The latest web installer allows for downloading an offline installation, that wasn't available when I installed 2017: