3

This code every time display Cat in debugger. First time and second time. But I want to display Cat at 1st time in debugger and then Dog in 2nd time debugger.

int main(){

    CStringArray arr;

    arr.Add("Cat");
    arr.Add("Dog");

    for (int i = 0; i < arr.GetSize(); i++)
    {
    cout<<"arr[i]"<<endl;
    }
    return 0;*
}   

Forget about cout I just used debugger.

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
Alex Cerry
  • 107
  • 13

3 Answers3

0

This is a simple code, it prints Cat and Dog. It won't print Cat, Cat, unless you have an error elsewhere. Are you looking for TRACE or OutputDebugString?

CStringArray arr;
arr.Add("Cat");
arr.Add("Dog");
for (int i = 0; i < arr.GetSize(); i++)
{
    const char *temp = arr[i];
    TRACE("%s\n", arr[i]);//add break point here to look at temp
    //or
    //OutputDebugString(arr[i]);
    //OutputDebugString("\n");
}

ps, I think you want to use breakpoints and look at arr[i] on the fly. You can use const char *temp = arr[i] it will make the nth element visible. Otherwise I don't know.

pss, debug feature Autos may not show the value at temp depending on where you put breakpoint, because it makes decisions automatically. But you should be able to look at it by just moving the mouse over to temp.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • No You Don't understand not in console in Autos/ Debugger. – Alex Cerry Apr 15 '15 at 05:00
  • First Time {m_pData=0x004166f0 L"Cat" m_nSize=2 m_nMaxSize=5 ...} mfc120ud.dll!CStringArray – Alex Cerry Apr 15 '15 at 05:03
  • Second Time {m_pData=0x004166f0 L"Cat" m_nSize=2 m_nMaxSize=5 ...} mfc120ud.dll!CStringArray – Alex Cerry Apr 15 '15 at 05:03
  • I Want Second Time Dog. That the problem? – Alex Cerry Apr 15 '15 at 05:04
  • 1
    The array's data buffer starts with L"Cat", at that address. If you add 1000 more elements and look at it 800 times it still starts with Cat. It's the same behavior with any array. If you want to debug then use a debug function and access the second element. – Barmak Shemirani Apr 15 '15 at 05:21
  • And How i do that please guide me? – Alex Cerry Apr 15 '15 at 05:24
  • Yes, I updated the answer to show what to do if using break points. Although I am guessing here about what you are trying to do. – Barmak Shemirani Apr 15 '15 at 05:27
  • I want to show 2 element one by one during run time in debug. Please write some code to solve my problem? – Alex Cerry Apr 15 '15 at 05:31
  • If you run that code I posted you have at least 3 different ways to look at arr[i], with `TRACE`, `OutputDebugString`, or break points – Barmak Shemirani Apr 15 '15 at 05:35
  • I try these three method nothing happen still just Cat is display in autos? – Alex Cerry Apr 15 '15 at 05:44
  • So this is a general question about debugging. Which version of Visual Studio do you have? Make sure "Output Window" is visible (View menu -> Output). The values are printed in Output Window in debug mode. In addition, you can put breakpoint in the right place, right after `temp`. In Autos window, you will see `temp=dog` in the second pass. Also when it breaks, move the cursor over `temp`, it should say "Dog" in the second pass. – Barmak Shemirani Apr 15 '15 at 06:26
  • Looks like you figured this out? I updated the answer to make few more notes. – Barmak Shemirani Apr 15 '15 at 13:46
0

Use

for (int i = 0; i < arr.Count(); i++)
    {
        cout<<arr.GatAt(i)<<endl;
    }

cout<<"arr[i]"<<endl; //this will print normal string i.e arr[i]

Himanshu
  • 4,327
  • 16
  • 31
  • 39
0

You can't do it in Autos or Locals views, but you can do it in Watch view. Add to Watch view arr.m_pData,3. Value 2 would be enough here, but let's see what happens if you put a larger number. When you expand the name in the Watch view you will be able to see three items. First two will be Cat and Dog and the third will most likely be '<Error reading characters of string.>'.

There is also a way to tell Visual Studio how to display CStringArray variables properly, but you'd have to make some changes, as described in another answer.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103