Why is this code showing no output?

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main()
{
char *abc = "((((((a+b)))";
char *s;
s = new char[strlen(abc)];
s= abc;
for (auto i = 0;i != '\0'; i++)
{
cout<<s[i]<<endl;
}
}

Its not even entering into the for loop;
Last edited on
Hey, I just read your introductory post (cause I wanted to make sure you weren't a filthy spammer before replying).

Welcome to the forums!

Learning to program has a huge learning curve, and you seem to be trying to jump over it. Much like trying to learn multiplication, you absolutely must spend time doing the boring part of memorizing the times tables. For C++, that means working your way through a few really simple and boring tutorials.

Since I am on my mobile I'll just correct the code with short commentary: [edit]Switched to PC because mobile typing is not fun.[/edit]

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
#include <cstring>
#include <iostream>
// any time you use anything in code you should check that you have the proper include at the top
// for cout, that's the iostream header
// for strlen(), that's the cstring header

int main()
{
  const char *abc = "((((((a+b)))";
  // String literals are encoded as CONSTANTS, so mark them as such.

  char *s = new char[strlen(abc) + 1];
  // Here you are creating a pointer (s) and allocating a new collection of characters that you
  // can MODIFY, and remembering its location using the pointer s.
  // Because strlen() only tells you the length of the string without the trailing null character
  // we must add it.

  // s = abc;
  // Don't do this. abc is a const char* string. s is a char* string. The const-ness matters.
  // The problem is that you are setting the POINTER value, NOT copying the string.
  // To copy the string, either use a loop, or use the convenient strcpy() function (also
  // found in <cstring>. There is plenty of room because we allocated enough for the entire
  // abc string plus the null terminator character:
  strcpy( s, abc );  // (copies abc to s and returns s)

  // for (auto i = 0;  i != '\0';  i++)
  // Before you were setting i to zero and then terminating when i was equal to zero
  // ('\0' is the same as (char)0).
  //
  // You were also unsure of the type of i. (It is an int.) Only use auto when you are 
  // dealing with a type you are unsure of / that is complicated and that the compiler
  // can resolve for you. In this case, you know what i should be, so auto is unnecessary.

  // Correct version 1: using an integer to index the array:
  // Indices range from 0 to +N; we stop when we have reached s[N] where s[N] == '\0' == (char)0
  for (int i = 0;  s[i] != '\0';  i++)
  {
    std::cout << s[i] << std::endl;
  }
  std::cout << std::endl;  // (added for readability)

  // Correct version 2: using a char* to index the array.
  for (char* p = s;  *p != '\0';  p++)
  {
    std::cout << *p << std::endl;
  }
  std::cout << std::endl;  // (added for readability)

  // Correct version 2, using auto and automatic boolean conversion for the test condition:
  for (auto p = s;  *p;  p++)
  {
    std::cout << *p << std::endl;
  }

  // Be sure to deallocate everything you allocate
  delete[] s;
}


[edit]
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
C:\Users\Michael\foo> cl /EHsc /Ox /std:c++17 /utf-8 a.cpp
...

C:\Users\Michael\foo> a
(
(
(
(
(
(
a
+
b
)
)
)

(
(
(
(
(
(
a
+
b
)
)
)

(
(
(
(
(
(
a
+
b
)
)
)

C:\Users\Michael\foo> _
I compiled using MSVC at the command line
(and skipped copying all the stuff that MSVC prints out)

I then ran the (successfully compiled) program "a.exe"

The first 12 lines are the first loop











The second 12 lines are the second loop












And the third 12 are the third loop

Hope this helps.
Last edited on
Actually I intentionally didn't include the headers in this question. My question is , why is it not even entering into the for loop.
Just got the solve. Its my silly mistake s[i] instead of i :/ uffh
I finished my post. Be sure to read through all the commentary.

Good luck!
Topic archived. No new replies allowed.