Counting letters

Newbie needing help

Page 1 of 1

9 Replies - 2017 Views - Last Post: 22 August 2006 - 04:43 AM Rate Topic: -----

#1 moegreen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-August 06

Counting letters

Posted 18 August 2006 - 05:10 AM

I'm writing a program that needs to recognice how many letters that the user has entered and store the number in an array.

Im a complete newbie here, been coding for about 4 days now.

I tried google, but not alot of the examples didnt make sense too me.

Can anyone help?

Thanks
Moe
Is This A Good Question/Topic? 0
  • +

Replies To: Counting letters

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Counting letters

Posted 18 August 2006 - 05:24 AM

The site has a policy by which we prefer to see a good faith effort on the part of the user before providing source code...can you post what you've tried so far? Can the user enter letters separated by spaces? Is the user prompted to enter input more than once?
Was This Post Helpful? 0
  • +
  • -

#3 help-linux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06

Re: Counting letters

Posted 20 August 2006 - 08:25 AM

View PostAmadeus, on 18 Aug, 2006 - 05:24 AM, said:

The site has a policy by which we prefer to see a good faith effort on the part of the user before providing source code...can you post what you've tried so far? Can the user enter letters separated by spaces? Is the user prompted to enter input more than once?


i am doing this off my head so there may be errors that need ironing out:

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

int main()
{
		 string hello;
		 size_t tmp;
		 cout << "Enter some text";
		 cin >> hello;
		 while(1)
		 {
				tmp = hello.find(" ");
				if(tmp == string::npos)
				{
						break;
				}
				else
				{
						hello.erase(tmp, 1);
				 }
		   }
		   cout << endl << "The amount of chars you entered:" << hello.length() << endl;
		   system("pause");
		   return 0;
}


Was This Post Helpful? 1
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Counting letters

Posted 20 August 2006 - 08:49 AM

Very nice, but for efficiencies sake, you can eliminate the check for spaces...the >> operator will stop accepting input at the first whitespace when taking it for a string. If you wished to include spaces, or allow for it, you'd be better off with the cin.getline() method.
Was This Post Helpful? 0
  • +
  • -

#5 help-linux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06

Re: Counting letters

Posted 20 August 2006 - 01:08 PM

i know how to use the >> operator but if i did:

char arr[100][40];
char check[40];
int i = 0;
while(i != 100)
{
	  somestring >> check;
	  if(check[0] == '\0')
	  {
			break;
	  }
	  else
	  {
		   arr[i++] = check;
	   }
	   check[0] = '\0';

}



would that work, as in, put all the words into "arr"?

i personnally using the find() function is better ;)
Was This Post Helpful? 0
  • +
  • -

#6 moegreen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-August 06

Re: Counting letters

Posted 22 August 2006 - 12:57 AM

Well, that helped alittle but not enough for me ;)

I've made a "Resistance colour calculator", where the user enters the four colours of a resistance and it then calculates the ohm.

but now i want to reverse it, and make the user enter a number(ohm) and get the prog to calculate and print out the colour code.

heres my first prog, now i need to reverse it, and theres is my problem.

First i need the user too enter the ohm, but since the resistance has a special colour code:

((1st * 10) + 2nd)) *(3rd number is then number of zeroes behind it)

Any suggestions?

Thanks, Moe

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <time.h>





void main(void)
{
	float band1,band2,band3, band4, total;
	char ohm1, ohm2, ohm3, ohm4, band3x;

   float
	black = 0,
   brown = 1,
   red 	= 2,
   orange= 3,
   yellow= 4,
   green	= 5,
   blue	= 6,
   violet= 7,
   gray  = 8,
   white = 9,
   gold  = 0.1,
   tnone = 20,
   tsilver = 10,
   tgold = 5;

	cout << "BLACK = A | BROWN = B | RED = C | ORANGE = D | YELLOW = E | GREEN = F | BLUE = G | VIOLET = H | GRAY = I | WHITE = J | GOLD = K |" <<endl;
   cout << "ENTER FIRST COLOUR CODE: ";
   cin >> ohm1;

//BAND 1
   	switch (ohm1)
   	{
   	case 'a':
	  band1 = black;
	  cout << "BLACK" << endl;
	  break;

   	case 'b':
	  band1 = brown;
	  cout << "BROWN" << endl;
	  break;

   	case 'c':
	  band1 = red;
	  cout << "RED" << endl;
	  break;

   	case 'd':
	  band1 = orange;
	  cout << "ORANGE" << endl;
	  break;

	  case 'e':
	  band1 = yellow;
	  cout << "YELLOW" << endl;
	  break;

   	case 'f':
	  band1 = green;
	  cout << "GREEN" << endl;
	  break;

	  case 'g':
	  band1 = blue;
	  cout << "BLUE" << endl;
	  break;

	  case 'h':
	  band1 = violet;
	  cout << "VIOLET" << endl;
	  break;

	  case 'i':
	  band1 = gray;
	  cout << "GRAY" << endl;
	  break;

	  case 'j':
	  band1 = white;
	  cout << "WHITE" << endl;
	  break;

	  case 'k':
	  band1 = gold;
	  cout << "GOLD" << endl;
	  break;

	  default:
	  clrscr();
	  cout << "Sorry, I did not recognise that colour - Please try again" <<endl;
	  break;
		}
//BAND2

   cout << "ENTER SECOND COLOUR CODE: ";
   cin >> ohm2;

   switch (ohm2)
   {
   	case 'a':
	  band2 = black;
	  cout << "BLACK" << endl;
	  break;

   	case 'b':
	  band2 = brown;
	  cout << "BROWN" << endl;
	  break;

   	case 'c':
	  band2 = red;
	  cout << "RED" << endl;
	  break;

   	case 'd':
	  band2 = orange;
	  cout << "ORANGE" << endl;
	  break;

	  case 'e':
	  band2 = yellow;
	  cout << "YELLOW" << endl;
	  break;

   	case 'f':
	  band2 = green;
	  cout << "GREEN" << endl;
	  break;

	  case 'g':
	  band2 = blue;
	  cout << "BLUE" << endl;
	  break;

	  case 'h':
	  band2 = violet;
	  cout << "VIOLET" << endl;
	  break;

	  case 'i':
	  band2 = gray;
	  cout << "GRAY" << endl;
	  break;

	  case 'j':
	  band2 = white;
	  cout << "WHITE" << endl;
	  break;

	  case 'k':
	  band2 = gold;
	  cout << "GOLD" << endl;
	  break;

	  default:
	  clrscr();
	  cout << "Sorry, I did not recognise that colour - Please try again" <<endl;
	  break;
   }
//BAND3

   cout << "ENTER THIRD COLOUR CODE: ";
   cin >> ohm3;

   switch (ohm3)
   	{
   	case 'a':
	  band3 = 0;
	  cout << "BLACK" << endl;
	  break;

   	case 'b':
	  band3 = 10;
	  cout << "BROWN" << endl;
	  break;

   	case 'c':
	  band3 = 100;
	  cout << "RED" << endl;
	  break;

   	case 'd':
	  band3x = 'K';
	  cout << "ORANGE" << endl;
	  break;

	  case 'e':
	  band3 = 10;
	  band3x = 'K';
	  cout << "YELLOW" << endl;
	  break;

   	case 'f':
	  band3 = 100;
		band3x = 'K';
	  cout << "GREEN" << endl;
	  break;

	  case 'g':
	  band3x = 'M';
	  cout << "BLUE" << endl;
	  break;

	  case 'h':
	  band3 = 10;
	  band3x = 'M';
	  cout << "VIOLET" << endl;
	  break;

	  case 'i':
	  band3 = 100;
	  band3x = 'M';
	  cout << "GRAY" << endl;
	  break;

	  case 'j':
	  band3x = 'G';
	  cout << "WHITE" << endl;
	  break;

	  case 'k':
	  band3 = 0.1;
	  cout << "GOLD" << endl;
	  break;

	  default:
	  clrscr();
	  cout << "Sorry, I did not recognise that colour - Please try again" <<endl;
		break;

	  }
//TOLERANCE
	cout << "NONE = A | SILVER = B | GOLD = C |" <<endl;
 	cout << "ENTER FOURTH COLOUR CODE: ";
   cin >> ohm4;

   switch(ohm4)
   {
   	case 'a':
	  band4 = tnone;
	  cout << "NONE" << endl;
	  break;

	  case 'b':
	  band4 = tsilver;
	  cout << "SILVER" << endl;
	  break;

	  case 'c':
	  band4 = tgold;
	  cout << "GOLD" << endl;
	  break;
   }


//RESULT
	  total = (band1*10+band2)*band3;
	  cout <<"RESULT: " << total << " " << band3x << " Ohms, +/- "<< band4 << "%" <<endl;

	  getch();
   }


Was This Post Helpful? 0
  • +
  • -

#7 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Counting letters

Posted 22 August 2006 - 02:33 AM

2 things (plz correct me if im wrong) about ure program.
1)for the third band, black is not zero but 10^0=1.
2)again for band three when u put orange, uve set band3x='K'. but u havent given band 3 any value. yet its part of ure final formula i.e. total = (band1*10+band2)*band3;
now when u dont give a variable any value at all, garbage values r stored in em. so when u declare the variables say:
float band3=1;


3)when u dont enter a correct band code, the program gives a message but continues. it doesnt ask for the code again.

This post has been edited by Louisda16th: 22 August 2006 - 03:30 AM

Was This Post Helpful? 0
  • +
  • -

#8 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Counting letters

Posted 22 August 2006 - 03:28 AM

Heres a suggestion for reversing ure program. suppose u enter 4567. firstly round the number down to 4600 since u can handle only 2 digits in a colour code. then convert to 46*100
#include<stdio.h>
void main()
{
	double resistance,c;
	int x, power,b;
	scanf("%lf",&resistance);
	/*code for finding 1st 2 digits.*/
	if (resistance>=100)
	{
		for(x=1;x<=15;x++)
		{
			resistance=resistance/10;
			if (resistance<100)
			{
				power=x;
				b=resistance;
				break;
			}
		}
	}

	else
		b=resistance;
/*code for rounding off(im not sure if there's a 
library function for rounding so im writing my own)*/
	c=resistance-b;//Since b only contains the integral part ure left with the fractional part
	
	if(c>=0.5)
		b++;

	printf("%d",b);
}



now when u have the 2 digits u require extract each digit and use a switch case statement to convert to equivalent colour.
int digit
digit = b/10; //This will give u the tens place
digit = b-(d*10);//this will give u da units place



hope this helps u out :)

EDIT: math.h not required.

This post has been edited by Louisda16th: 22 August 2006 - 05:21 AM

Was This Post Helpful? 0
  • +
  • -

#9 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Counting letters

Posted 22 August 2006 - 03:33 AM

View PostLouisda16th, on 22 Aug, 2006 - 03:03 PM, said:

2 things (plz correct me if im wrong) about ure program.
1)for the third band, black is not zero but 10^0=1.
2)again for band three when u put orange, uve set band3x='K'. but u havent given band 3 any value. yet its part of ure final formula i.e. total = (band1*10+band2)*band3;
now when u dont give a variable any value at all, garbage values r stored in em. so when u declare the variables say:
float band3=1;


3)when u dont enter a correct band code, the program gives a message but continues. it doesnt ask for the code again.

Here although the float declaration would work, it is advisable to initialise it to a float value to prevent the compiler from automatically typecasting it.
Hence
float band3=1.0;
would be a better idea.
Was This Post Helpful? 0
  • +
  • -

#10 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Counting letters

Posted 22 August 2006 - 04:43 AM

one more thing.. u dont require all the header files uve included. As far as i can see, u require only iostream. and conio.h. Also about the switch statement uve used. put it in a function and use it again when required. u dont need 2 retype it then :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1