C++ string.at(0)

Hi, guys, new girl here :-) needs your help.

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.


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
57
// it works - string.at(0) for first character
#include <iostream>
#include <string>

using namespace 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>

using namespace 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;
}
Last edited on
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.

Last edited on
closed account (E0p9LyTq)
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.
Last edited on
Ok. braces and vectors. Tnx

The compiler was https://www.onlinegdb.com/online_c++_compiler
Last edited on
closed account (E0p9LyTq)
I personally despise online compilers, I prefer using a compiler/IDE installed on my computer(s).

With that said, there are "better" on-line compilers that would at the very least give a warning using a VLA is against the standard.

There is one here at cplusplus: http://cpp.sh/
It only warns, it still compiles your 2nd program

http://coliru.stacked-crooked.com/ will show errors and halt compilation, as a good compiler should.
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.

Exactly. Its not the age, its the settings, for anything that is new enough to support '17
closed account (E0p9LyTq)
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:

https://docs.microsoft.com/en-us/visualstudio/install/create-an-offline-installation-of-visual-studio?view=vs-2017
If you are using Windows consider Visual Studio 2017. It is free in the Community edition and has a decent built-in debugger.

Thank you very much :-)
Topic archived. No new replies allowed.