Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,183 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,391 people online right now. Registration is fast and FREE... Join Now!




char help

 
Reply to this topicStart new topic

char help

warxc++
8 Jul, 2008 - 12:29 PM
Post #1

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 2

I am having a char problem. I dont't understand what i am doing wrong. I was reading tutorials and understand. So i tried experimenting but i couldn't input a word like "hello". every time i input the program closes. What's wrong with my code cry2.gif NEED HELP cry2.gif

CODE
#include <iostream>

using namespace std;

char main()
{
  char i;
  cin>> i;
  cin.ignore();
  cout<< i <<"\n";
  cin.get();
}


This post has been edited by warxc++: 8 Jul, 2008 - 12:31 PM
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Char Help
8 Jul, 2008 - 12:44 PM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
Slight correction
CODE
#include <iostream>

using namespace std;

int main()
{
  char i;
  cin>> i;
  cout<< i <<"\n";

  cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
  cin.get();

  return 0;
}


You can learn more about Holding The Execution Window here.
User is offlineProfile CardPM
+Quote Post

Sn0wm4n
RE: Char Help
8 Jul, 2008 - 12:48 PM
Post #3

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 12


My Contributions
I only know the basics of C++ mostly but first of all, "char main" needs to be "int main" and char means character if I'm not mistaken. It won't accept "hello" because it is 5 char's. Now if you wanted to use char with hello, it would have to be a certain letter like i[0] for the first letter or i[4] for the 5th letter (0 is 1st letter, 1 is 2nd letter, 2 is 3rd letter, and so on). Now if you wanted to store "hello" in a variable, you would have to use #include <string> and store a word in string.
CODE
#include <iostream>
#include <string>

using namespace std;

int main() {
//make the string
string test;
//get a value for test
cin >> test;
//show the value on the console
cout << test << endl;
//wait for your response
system("PAUSE");
}



This post has been edited by Sn0wm4n: 8 Jul, 2008 - 12:50 PM
User is offlineProfile CardPM
+Quote Post

captainhampton
RE: Char Help
8 Jul, 2008 - 01:09 PM
Post #4

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
QUOTE(Sn0wm4n @ 8 Jul, 2008 - 01:48 PM) *

I only know the basics of C++ mostly but first of all, "char main" needs to be "int main" and char means character if I'm not mistaken. It won't accept "hello" because it is 5 char's. Now if you wanted to use char with hello, it would have to be a certain letter like i[0] for the first letter or i[4] for the 5th letter (0 is 1st letter, 1 is 2nd letter, 2 is 3rd letter, and so on). Now if you wanted to store "hello" in a variable, you would have to use #include <string> and store a word in string.
CODE
#include <iostream>
#include <string>

using namespace std;

int main() {
//make the string
string test;
//get a value for test
cin >> test;
//show the value on the console
cout << test << endl;
//wait for your response
system("PAUSE");
}

Don't forget to add "return 0; " at the end of the code




User is offlineProfile CardPM
+Quote Post

realNoName
RE: Char Help
8 Jul, 2008 - 02:05 PM
Post #5

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 300



Thanked: 5 times
My Contributions
if you still want to use char's you can use an array

Link to nice tutorial on arrays

CODE
#include <iostream>
using namespace std;

int main()
{
    char myText[10];

    cin >> myText;

    cout << myText << endl;

    return 0;
}

User is offlineProfile CardPM
+Quote Post

polymath
RE: Char Help
8 Jul, 2008 - 05:12 PM
Post #6

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 415



Thanked: 4 times
Dream Kudos: 500
My Contributions
Just a note, if you don't want to use cin.ignore, you can use:

cpp

char InputChar () {
char returnVal;'
returnVal=cin.get();
while (cin.get()!='\n');
return (returnVal);
}

/*To be used like:

char character;
character=InputChar();

*/


Of course, I would always look into using the string library:

cpp

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

int main() {
string input;
getline(cin, input); //easiest input for strings
cout << input << endl << "Press any key then enter to exit. ";
cin >> input;
return 0;
}


String library makes our lives much simpler. Now, to make it so that our input isn't a cin, but a cin.get(),

cpp

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

int main() {
string input;
getline(cin, input); //easiest input for strings
cout << input << endl << "Press any key to exit. ";
cin.get();
return 0;
}


For future reference: The getline function in the string library when used that way will clear the input buffer for you, so you can just use getline and a dummy string instead of cin.ignore, just to say.

This post has been edited by polymath: 8 Jul, 2008 - 05:15 PM
User is offlineProfile CardPM
+Quote Post

warxc++
RE: Char Help
9 Jul, 2008 - 04:18 AM
Post #7

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 2

k thx everyone biggrin.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 11:19AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month