HELPPP cin.get() confusion ONLY BEGINNER

need to find a way to achieve the same outcome as cin.get() but using

Page 1 of 1

8 Replies - 1767 Views - Last Post: 03 May 2008 - 04:54 AM Rate Topic: -----

#1 yukapuka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-May 08

HELPPP cin.get() confusion ONLY BEGINNER

Posted 02 May 2008 - 06:11 AM

// This program counts the number of characters input
// by the user on one line. The line is terminated by
// a return. The return is not counted as a character.

#include <iostream>

using namespace std;

int main()
{
char ch; // Used to store the input character
int num_chars = 0; // Counts the number of characters
cout << "Enter any number of characters terminated by return."
<< endl << endl;

ch = cin.get(); // Obtain the first character

while (ch != '\n')
{
++num_chars;
ch = cin.get();
}

cout << endl;
cout << "You entered " << num_chars << " characters." << endl;

return 0;
}


thats the original program , now need to achieve this:

recode using cin and the extraction operator in place of cin.get().

PLZ HELPPPPP IM GOIN CRAZY TRYING TO FIGURE IT OUT
PLZ ANSWER ON A BASIC LEVEL
THANKS

Is This A Good Question/Topic? 0
  • +

Replies To: HELPPP cin.get() confusion ONLY BEGINNER

#2 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: HELPPP cin.get() confusion ONLY BEGINNER

Posted 02 May 2008 - 08:44 AM

Please place your code in the [*code] code here [*/code] tags.
Without the asterisk "*". It makes it easier to read and its forum rules.

Also, it appears thet you put way to much info in either your topic description or title. Please, keep it short, and include specifics in the post itself.

Live long and code strong.

This post has been edited by Delta_Echo: 02 May 2008 - 08:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 53
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: HELPPP cin.get() confusion ONLY BEGINNER

Posted 02 May 2008 - 01:43 PM

Use the code tags like Delta_Echo said. For c++ you can also use [*code=cpp] and [*/code] (without the astrixes).

That aside...

Here's something that might help! If you need to use specifically "cin >>", that'd be hard, because cin counts any blank space character as the end of the string. Hope this helps!

#include <iostream>
using namespace std;

int main () {
	char a[201]; //creates char array to act like a string
	int b; //a var to store the number of chars
	string exit; //creates a string to imput at the end
	b=0;
	cout << "type a string (200 char max) :" << endl;
	cin.getline (a, 200); //stores a value to a[]
	while (b<201) { //as long as b is less than the MAX:
		if (a[b]!='\0') { //and the char being examined is non-null
			b++; //increment b
			continue; //and skip to the next iteration of the loop)
		}
		b--; //decrement b if char analyzed is null (else skipped by continue)
		break; //cancel the loop
	}
	cout << "You typed " << b << " characters." << endl; //outputs b
	cout << “Press any key followed by enter to exit. “;
	cin >> exit;
}



If you need further explanation of if this doesn't work/satisfy your requirements, im sorry (if you want you can PM me).

Have a great day!

This post has been edited by polymath: 02 May 2008 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 03-March 08

Re: HELPPP cin.get() confusion ONLY BEGINNER

Posted 02 May 2008 - 02:02 PM

Just a sidenote, there's a neat little icon in the posting window that will put the code tags for you.
Was This Post Helpful? 0
  • +
  • -

#5 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 53
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: HELPPP cin.get() confusion ONLY BEGINNER

Posted 02 May 2008 - 03:20 PM

View Postjeronimo0d0a, on 2 May, 2008 - 02:02 PM, said:

Just a sidenote, there's a neat little icon in the posting window that will put the code tags for you.


True, but there's no code button that will include special code highlighting and styling.

A note to yukapuka:
Everyone else is right, just use code tags, and add details in the body of your post. The title and description should only give a general idea of the type of problem you are having to attract the right kind of people to your post to answer your question. For this post, your title was too specific and long, "c++ problem" is to broad, but "Trouble using cin--HELP" is just about right. Sorry if I sound preachy.

Happy coding!
Was This Post Helpful? 0
  • +
  • -

#6 yukapuka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-May 08

Re: HELPPP cin.get() confusion ONLY BEGINNER

Posted 02 May 2008 - 05:51 PM

Thanks Alot to those who contributed to the answer and specifically, PolyMoth , i appreciate your expetise.

What i have gathered from your answer is that you had to include the cin.get (code?) to be able to structre that program, i was wondering tho disregarding the cin counting the spaces, what would be a way to contsruct the program using only cin and giving a blind eye to the space counting included in the counting of cin stream
Was This Post Helpful? 0
  • +
  • -

#7 yukapuka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-May 08

Re: HELPPP cin.get() confusion ONLY BEGINNER

Posted 02 May 2008 - 06:46 PM

sorry ****PolyMath
Was This Post Helpful? 0
  • +
  • -

#8 yukapuka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-May 08

Re: HELPPP cin.get() confusion ONLY BEGINNER

Posted 02 May 2008 - 06:55 PM

i would have PM you PolyMath but it wouldnt allow me to PM you keeps saying some error :/
Was This Post Helpful? 0
  • +
  • -

#9 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 53
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: HELPPP cin.get() confusion ONLY BEGINNER

Posted 03 May 2008 - 04:54 AM

Expertise!? I'm flattered.

Anyway, there are two things i think you could try, though i couldn't help you as much here. The first is to check the istream and see what character was in it last (and terminated cin), and if its a space, imput a second string and loop it until the loop is broken by a break statement for if it was terminated by a '\n'. The second is using the std::noskipws modifier with cin, and although i'm not sure on the syntax, and i'm not sure it would work, you could give it a try. IF you were to try either of these you would need to talk with somebody who specializes in the area of istream.

Sorry for the long wait for a reply, and that i'm not so good with streams (I take them for granted, kinda).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1