If you've learned about pointers, have you learned about dynamic memory allocation, i.e. malloc/free. That would allow you to read a string into memory without technically using an array.
Also, many profs provide helper libraries/ for students to use in all there programs. Is there such a library/code for this class, that might provide a function called something like ReadString.
I see your point, but here we are with this problem - what can you do to hold the first name, while the last name is input? Having 20 or so individual chars doesn't seem keen.
If the instructor hasn't given a hint on how this should be done, the students need to press said instructor, for same.
Does the first name HAVE TO be given to the program first? If the order could be changed to lastname given first, then firstname, it would be simpler.[/QUOTE]
Assignment.
You are to ask a user for first name followed by last name.
Example:
Lisa Smith
Your program should get this data and make an e-mail like this
[email protected]
It's stupid, right? He won't let those of us who know more than he has taught to use what we know! He wants us to use a do while loop, I think. And the getchar() function. If you see in my code, I think I am to replicate what I used for the last name (which printed perfectly), for the first name as well. However, when I replicate the do while loop, I get stuck in a static but infinite loop, so whenever I input something, nothing happens, and the program keeps running. This is what the assignment says. While he doesn't say anything about not using arrays, he said we can only use what we have learned by now and the last time I used arrays, he made me redo the assignment. Thank you for al your help
That's the trick, isn't it? Perhaps not storing more than one character at a time in the first place while doing everything sequentially. Or store the characters in a file rather than the usual array. Removing a common tool makes for some interesting, if convoluted, solutions.what can you do to hold the first name, while the last name is input?
We haven't learned about strings either. We are to learn about arrays next week, immediately after the assignment is due. The reason for this unusual teaching method is because we are to learn the entire C (not c#, c++, just basic c) language in a six week summer semester, and we are using a book from the 90s.
I think I am to use what I used for the last name for the first name as well. But when I try, it doesn't give me the desired result. If anyone assisting me on this forum can help me replicate my loop, it would be very well appreciated. Thanks!
It can be done with enough chars to hold that first name, while the last name is input. You may need enough chars for the last name as well, depending on the suffix you need to print. Without the chars, you have no way to store the letters. (There are ways, but not at this entry level, so you can't use them)
Use a while loop, and put all it's letters, into chars, using getchar(). Then get the last name, and print it, then the underline and first name, and finally the suffix: @whatever.com
You will need two separate while loops - one for each name. Each will use getchar() and store the letters into separate chars:
f0,f1,f2,f3,f4,f5,f6 ... f20
l0, l1,l2,l3,l4,l5,l6 ... l20
f="first"
l=el, for "last".
Last edited by Adak; 06-28-2013 at 02:28 PM.
Thank you very much! By while loop, do you mean the standard while loop, or the do-while loop I used for the last name? Also, when I put the printf statemnet for the last name before the one for the first name, the first name is still printed first. Can you explain why that is? Thanks
do while is fine, and preferred here.
SEPARATE THE CHARS for first and last names.
Alright, thank you very much. I will attempt to fix it and see if it works
I'm sorry to bother you, but it is still not functioning. I created a new variable, assigned it get char, and created a do while loop for the first name and placed it below the last name and the _first letter. However, I do not know what to put in the while statement of the do while, do to the fact that while((l=getchar())!='\n'); has been used for the lastname, and I don't want the user to hit enter twice. And while( (l=getchar())!=' '); also, neither end up printing past the first letter. If possible, can you view my original code and specifically explain what I need to change! Thank you!!
OK, the first way I suggested -- forget it! It's workable, but the result is so insanely backward, it can't possibly be what your instructor wanted. I don't see any way to make a C program, using nothing but char and int variables, and nothing else. Except this:
Look at this, and see if it might be what was intended. It uses a FILE variable, but this is as close as I can get atm.
Code:#include<stdio.h> int main() { FILE *fp=fopen("firstname.txt", "w"); int ch; printf("Enter the first name: \n"); while((ch=getchar()) != '\n') { //write first name to file for storage fputc(ch, fp); } fprintf(fp, "@blank.com\n"); fclose(fp); printf("Enter the last name: \n"); while((ch=getchar()) != '\n') { //print name from keyboard input printf("%c",ch); } putchar('_'); fp=fopen("firstname.txt","r"); //print name from file input while((ch=fgetc(fp)) != '\n') { putchar(ch); } putchar('\n'); fclose(fp); return 0; }
Thank you very much! Would this assignment be any easier to complete?
Assignment.
You are given a file named data.txt. This file contains first name followed by last name.
Example:
Lisa Smith
Your program should get this data and make an e-mail like
[email protected]
And place them in output file named email.txt
This is the second assignment we are to do, though we have extended time on this, because we haven't learned to deal with files yet. To make our job "easier" (**sarcasm intended) our professor changed it to the assignment I originally showed you, which is due this Monday, where we were to only use loops and get char() functions.
Thanks once again
The second assignment would be almost exactly the same. Try it and I believe you'll see that.
I have a strong dislike for these "work with a blindfold on, and both arms shackled behind your back, and 50 kgs of chain around your feet", type of problems. I'm curious exactly what the instructor wanted on this first program.
You're welcome.