• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

EASY POINTS: Newbie Question. cin only seems to assign the first character entered to the strnig variable

Hi all,

I just started to learn C++ so bare with me.  In this program, when it asks me for my name, I enter "Adam", then press enter.  I'm expecting the screen to then print "Adam, you're amazing!", but it doesn't.  What i see is "A, you're amazing".  only the first byte of the string i enter is going into the variable myname. Why? and whats the fix?
// Testing.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
char myname;
 
 
 
int _tmain(int argc, _TCHAR* argv[])
{using namespace std;
cout << "Whats your name?\n";
cin >> myname;
cout << "\n";
cout << myname << ", you're amazing!\n";
 
system("pause");
	return 0;
}

Open in new window

0
ducky801
Asked:
ducky801
  • 5
  • 4
1 Solution
 
evilrixSenior Software Engineer (Avast)Commented:
The probem is char myname; is only a type for 1 character, try changing it to char myname[100], which will allow for up to 100 chars.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> try changing it to char myname[100]
Or better still, more C++ like and safer use a std::string

http://www.cplusplus.com/reference/string/string/


// Testing.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
 
#include <string>
 
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
   string::myname;
   cout << "Whats your name?\n";
   cin >> myname;
   cout << "\n";
   cout << myname << ", you're amazing!\n";
 
system("pause");
        return 0;
}

Open in new window

0
 
ducky801Author Commented:
Great!  That worked.  I knew it would be something small.  One more thing before I assign points then:
What if i don't know what the length of my string and i want to allow it to be the maximum that is 'binarily' possible?
 
AR
 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
evilrixSenior Software Engineer (Avast)Commented:
>> What if i don't know what the length of my string and i want to allow it to be the maximum that is 'binarily' possible?

use std::string, see my 2nd post. It'll dynamically grow. You can't dynamically grow a fixed size array (clue is in the name :) )
0
 
ducky801Author Commented:
The program broke on string::myname;  I assume its because i'm using the microsoft compiler??
0
 
ducky801Author Commented:
Thanks for the help!
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> he program broke on string::myname;  I assume its because i'm using the microsoft compiler??
Nope, thats cos I can't type :)

try...

string  myname;

I must have accidentally typed :: between the string and the name... sorry :)
// Testing.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
 
#include <string>
 
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
   string  myname;
   cout << "Whats your name?\n";
   cin >> myname;
   cout << "\n";
   cout << myname << ", you're amazing!\n";
 
system("pause");
        return 0;
}

Open in new window

0
 
ducky801Author Commented:
Success!  Thanks again!
0
 
evilrixSenior Software Engineer (Avast)Commented:
No worries... sorry I can't type :)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now