I need help with using the cin command\function. Any time I try to use it outside of the main function it does not work what am I doing wrong?

Recommended Answers

All 9 Replies

Just makin sure it worked.

Hmm.. looks like you really new to programming.

"main()" as you say is the entry point for the program you write. Syntactically and technically you cant write anything outside main except macros, function definations and declarations, and global variables.

So if you want to write "cin" outside main create your own function and then write cin anywhere in that function you want.

By the way, what are you trying to achieve? IF you are a complete newcomer to prog then try out the sticky or thread "Starting C" which will help you out in getting some programming concpets cleared. Also look at these sites.

http://www.cprogramming.com/tutorial/lesson1.html
http://www.cplusplus.com/doc/tutorial/

I did make my own functions, but when i tried to use the 'cin' command my compiler compiled, but the compiled program simply skipped the command and went to the next one. Yes I am a very new programmer reading a 9 year old book (whether that matters, or not i don't know. Thanks for the response and any future help you may bring.

1) Post some code. Unfortunately, crystal balls are a bit out of the budget, so we can't see what's wrong.

2) A 9-year old book is... well, almost antiquated. Consider getting a new one, since C++ has been standardized since your book was published. Some compilers may have problems with old code. If your compiler is nearly as old as your book, I'd recommend getting a new compiler as well.

I forgot one detail...I can use cin on numeric data, but i cannot use it on text (except in main()) Scratch the above...I can use cin just not cin.get...why?

1 Some code:
/* Though my book is out-dated my compiler is new (Bloodshed 4.9.9.2, and i am running on a Windows 98 OS if that has any part in my misfortune. */

#include <iostream>
    #include <cstdlib> //What does this header do?
    #include <iomanip.h> //is the .h really neccesary  
 
    using namespace std; //what does that do?  
 
    void TestCin(/*What goes here and why?*/);
    int main(int argc, char *argv[]) //What is the stuff in ()'s do?
    {
        char TestCharGet[21];//Create TestCharGet as a character array
        cout << "Test the cin command\n";//output info
        cin >> TestCharGet;//Get TestCharGet
        cout << "\n" << TestCharGet << "\n";//output input
        system("pause");//Pause for viewing of inputed info
        TestCin();
    }
 
    void TestCin()
    {
         system("cls");//Clear the Screen
         char Toop[21];//Toop has no meaning, just a variable
         cout << "does it work\n";//That is the question.
         cin.get(Toop,21);//cin >> Toop works, .get does not.
         cout << "\n" << Toop << "\n";//Did it work?
         system("pause");//Find out next time on mario returns.
    }

I think you skip the next cin because you still have the enter pressed.

try putting cin.ignore(); in front of it.

Edit: Do you to want answer on the questions in the code?

I forgot one detail...I can use cin on numeric data, but i cannot use it on text (except in main()) Scratch the above...I can use cin just not cin.get...why?

1 Some code:
/* Though my book is out-dated my compiler is new (Bloodshed 4.9.9.2, and i am running on a Windows 98 OS if that has any part in my misfortune. */

#include <iostream>
    #include <cstdlib> //What does this header do? LOOK HERE
    #include <iomanip.h> no .h is not needed
 
    using namespace std; //construct in C++ to avoid name clashes and 
                                    // to organize modules in a logical way 
 
    void TestCin(/*What goes here and why?*/);

    int main(int argc, char *argv[]) 
// argc - number of command line arguments
// agrv - array of char pointers -- basically array of C strings which
// are used to store the parameters supplied at teh command line
    {
        char TestCharGet[21];//Create TestCharGet as a character array
        cout << "Test the cin command\n";//output info
        cin >> TestCharGet;//Get TestCharGet
        cout << "\n" << TestCharGet << "\n";//output input
        system("pause"); // dont use non portable like this one
                                  // instead use getchar() for the same thing
        TestCin();
    }
 
    void TestCin()
    {
         system("cls"); // dont use non standard functions which make
                              // your code non portable

          char Toop[21]; 

         cout << "does it work\n";

         cin.get(Toop,21); // cin >> toop not safe ssince no bound
                                  //  check. You overwrite the mem that
                                 // doesnt belong to you

         // if you using C++ better use string instead of char arrays
        // which are unsafe.  

         cout << "\n" << Toop << "\n";//Did it work?
    }

Hope it helped, bye.

Thanks for all the help so far, but I am a beginner and may have many more questions to come. How can I clear the screen (I knew my commands could make the program unusable on some computers without those, but I had no idea how to get around it.) How do i create a string, and why not simply use a character array? Also on a somewhat different subject, I am 14 years old and would like to know if i am getting a headstart at all. Thanks again,Brent.tc at email removed

Thanks for all the help so far, but I am a beginner and may have many more questions to come. How can I clear the screen (I knew my commands could make the program unusable on some computers without those, but I had no idea how to get around it.)

Don't. It's not necessary except in few cases, and it's not protable from system to system or compiler to compiler. Just output a few extra lines to separate output sections.

How do i create a string, and why not simply use a character array?

Instead of char varName[100]; use string varName; You can still access individula characters by varName[i]

Also on a somewhat different subject, I am 14 years old and would like to know if i am getting a headstart at all. Thanks again,Brent.tc at email removed

If you know a lot of 14 year olds programming C++, then no. I don't so I'd have to say yes...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.