Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 13th, 2004, 4:47 PM   #1
shadowhunter
Programmer
 
shadowhunter's Avatar
 
Join Date: Dec 2004
Location: Scotland
Posts: 66
Rep Power: 4 shadowhunter is on a distinguished road
Can anybody out there please help me?

For uni module I got to modify a program to enable initial capitalisation no matter what mix of cases is used.

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

char sname [12];
char fname [12];
char address [25];
char town [25];
int age;

void main()

/* This piece of code is used to gather information from the user. */
{

	printf("\nEnter surname: ");
	scanf("%s", sname);
	printf("\nEnter first name: ");
	scanf("%s", fname);
	printf("\nEnter age: ");
	scanf("%d", &age);
	printf("\nEnter address: ");
	scanf("%s", address);
	printf("\nEnter town: ");
	scanf("%s", town);
	printf("\n");

	putchar(201);
	printf(" * * * * * * * * * * * * * * * * ");
	putchar(187);
	printf("\n");
	printf("\n");
	printf(" * *");
	printf("%s", fname);
	printf(" ");
	printf("%s", sname);
	printf(",");
	printf(" ");
	printf("Age: %d", age);
	printf(",");
	printf(" *");
	printf("\n");
	printf(" * *");
	printf("%s", address);
	printf(",");
	printf("\n");
	printf(" * *");
	printf("%s", town);
	printf(" ");	
	printf("\n");
	printf("\n");
	putchar(200);	
	printf(" * * * * * * * * * * * * * * * * ");
	putchar(188);	
	printf("\n");
}

I cannot program for all the sand in the sahara. I need this done immediately and I cannot get coding to work properly. Tried puting in the coding for capitalisation but it locks up and crashes.

Thanks, shadowhunter.
shadowhunter is offline   Reply With Quote
Old Dec 13th, 2004, 5:42 PM   #2
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
I'm not sure if this is exactly what you need, but it should be close enough to give you the idea of what you should be doing:

#include<stdio.h>

int main() {
 char sname[12];
 char fname[12];

 printf("Enter Surname: ");
 scanf("%s", sname);
 printf("Enter First Name: ");
 scanf("%s", fname);

 if(sname[0]>=90) {
  sname[0]-=32;
 }

 if(fname[0]>=90) {
  fname[0]-=32;
 }

 printf("Firstname: %s\nSurname: %s\n\n", fname, sname);

 return 0;
}

Good luck!

Edit: In case you're wondering where the >= 90 and the -30 came from, all the lowercase letters are represented by numbers from 97 to 122, the capitols from 65 to 90. Hence, to get from one to another, you subtract or add 32 (97-65=32)
These numeric representations for letters are called ASCII codes. You can check out the ascii character code table here: The ASCII table
ZenMasterJG is offline   Reply With Quote
Old Dec 13th, 2004, 6:11 PM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Also, under Ctype.h there is a function called toupper() which makes a character uppercase.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Dec 13th, 2004, 6:12 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Fixed code tags in the original post.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Dec 13th, 2004, 6:52 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Here's some simple code to set a word to Title case:
if ((string[0] >= 'a') && (string[0] <= 'z')) {
   string[0] -= 32;
}
for (int i = 1; string[i]; i++) {
   if ((string[i] >= 'A') && (string[i] <= 'Z)) {
     string[i] += 32;
   }
}
See if you can decipher it.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 14th, 2004, 6:46 PM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Create a function that accepts a string as a parameter. Inside the function... compare the ascii value of the first position in the string... if not uppercase, do the necessary calculation on the ascii value... using strcat build a new temp string... then return the temp string to the calling function... in this case, it would be the main function right before you wanted to print the results of the names.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Dec 14th, 2004, 8:25 PM   #7
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
Making a function that builds a temp string seems a bit heavy handed for a little program like this, i think. Though it sure is better coding pratice then when i pretty much just cut-and-pasted. I tell my friends never to cut and paste code but I'm guilty of it too often. Damn being a hypocrit...
ZenMasterJG is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC