0

How would I recall, say, the fourth character and output it on the screen from this code?

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


int main () {
    cout << "Enter word: ";
    char random[99];
    cin >> random;
    \\right here is where I would like to output the fourth character of the string "random"
    return 0;
}
  • 7
    `cout << random[3] << endl;` – Mysticial Dec 28 '11 at 22:19
  • 1
    Seems like a valid question to me. – Marlon Dec 28 '11 at 22:21
  • thanks. when i was testing this i was using strings that had four characters and forgot that there was a zero value as well –  Dec 28 '11 at 22:21
  • `cin >> random;` Right there you have a buffer overflow bug. Why aren't you using `std::string`? Get [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)! – sbi Dec 28 '11 at 22:32

1 Answers1

4
cout << random[3];

Consider reading a good book on C++

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Op does not need the statement he needs an understanding of how arrays work. – Captain Giraffe Dec 28 '11 at 22:22
  • 2
    @Captain Giraffe: Exactly, and for that he needs to read a book on C++ – Armen Tsirunyan Dec 28 '11 at 22:26
  • I'm teaching myself C++ using mostly this site and cplusplus.com. Sorry if I ask stupid questions, but it's the only way I'll learn. –  Dec 28 '11 at 22:29
  • 1
    @Redmasti: You will have next to no chance to properly learn C++ without [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – sbi Dec 28 '11 at 22:31
  • 1
    @sbi: Thanks for the advice. I'll definitely try to obtain a good book. –  Dec 28 '11 at 22:32