LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2005, 04:05 AM   #16
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239

oooops! didn't test

strcat changed:


Code:
#include<stdio.h>

 char firstName[512];
  char lastName[512];
  char fullName[1024];

int main()
{


   

    printf("Please enter your first name: ");
    scanf("%s", firstName);

    printf("Please enter your last name: ");
    scanf("%s", lastName);

    sprintf(fullName, "%s %s", firstName, lastName);          //This is supose to assign both the firstName[] and lastName[] string arrays to the fullName[] string array.


    printf("%s\n", fullName);


    getchar();

}

Last edited by bigearsbilly; 09-21-2005 at 05:00 AM.
 
Old 09-21-2005, 04:29 AM   #17
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
hmm

just a little question
what does fflush(stdin); ??
fflush flushes an output buffer
but stdin is input
so it should just do nothing, not?
 
Old 09-21-2005, 05:01 AM   #18
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
'tis removed.
good spot.
(cut and pasted)
 
Old 09-21-2005, 06:06 AM   #19
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
I know that when you want to put a value into a variable using scanf you need to use the & operator like:
Code:
scanf("%d", &age);
. What other data types do you need to use the & operator for when using the scanf function?

Also do I need to use the & operator for arrays when accepting string input using scanf?
 
Old 09-21-2005, 06:14 AM   #20
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
for an array don't need the &
the name is the address.

e.g:
char buffer[100];

buffer same as &buffer[0]

Last edited by bigearsbilly; 09-21-2005 at 06:15 AM.
 
Old 09-21-2005, 06:43 AM   #21
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by bigearsbilly
for an array don't need the &
the name is the address.

e.g:
char buffer[100];

buffer same as &buffer[0]
So I would need the & for char, float, int and double and that's all?

I know that the & symbol before a variable like:
Code:
&myAge;
is a symbol that is reffered to as the "Address Of Operator", which is used for pointers as I have learnt this from C++. Anything I need to know about this in C or is that all?

Thank Yous For You Time
 
Old 09-21-2005, 07:30 AM   #22
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
correct.

trying getting familiar with a debugger.
'gdb' with 'ddd' is particularly good. you can play about and see
what's really happening in there. It all helps gain an understanding.
you can even call functions etc.
 
Old 09-22-2005, 12:29 AM   #23
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Have another question. What I am trying to do with the below program is to not print any of the array on the last string literal, but still have the %4s in the string.:


Code:
/*
*Trying to work out how I would print the %s, in a string. I know in C++ it will
* print if you just put it in single quotes, for example '%s'.
*
*/
#include<stdio.h>


int main()
{
    char myFirstName[15];

    printf("Please enter your name: ");

    scanf("%s", myFirstName);

    printf("\nYour first name is %s\n", myFirstName);
    printf("How do get this to print and not the array? >> '%4s' ");


    fflush(stdin);
    getchar();



}
 
Old 09-22-2005, 12:33 AM   #24
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
printf("How do get this to print and not the array? >> '%%4s' ");

PS:
Leaving out the "#include <string.h>" (from the earlier version, when you had "strcat()") and failing to return a value from "main()" (still in the current version) are both arguably bugs. At best, they're poor form.

The "fflush()" is harmless, but unnecessary. The C library automatically flushes any output pending on "stdout" before accepting input from "stdin". The third stream, "stderr", is always unbuffered and, consequently, never needs to be flushed.

As a previous poster suggested, the debugger (e.g. "gdb") can be your friend. On gcc, debugging support is enabled via the "-g" compiler switch.

Another good practice is to always compile with "full warnings". On gcc, this is the "-Wall" switch (actually, there are also other, even stricter switches you could add).

Here's what I got compiling your program with "-g" and "-Wall":
Quote:
cc -g -Wall xx.c
xx.c: In function `main':
xx.c:18: warning: too few arguments for format
xx.c:26: warning: control reaches end of non-void function

Last edited by paulsm4; 09-22-2005 at 12:42 AM.
 
Old 09-22-2005, 01:12 AM   #25
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Code:
printf("How do get this to print and not the array? >> '%4s' ");
%4s indicates that there's a parameter to print.
e.g
Code:
printf("How do get this to print and not the array? >> '%4s' ","bye bye");
// or from your code
printf("\nYour first name is %s\n", myFirstName);
 
Old 09-22-2005, 02:08 AM   #26
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
i think InivisbleSniper wanted to know this:
printf("How do get this to print and not the array? >> '\%4s' ");


in C you have to use a backslash for the %
if you do
printf("\%"); it will print the %
printf("\\"); will print a \
and so on ....

if you aren't sure ask yourself this question:
if i want to print
%4s
how does the printf function know if i want to print %4s or a string?
it's the backslash who makes the difference *g*
 
Old 09-22-2005, 03:30 AM   #27
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
this?

Code:
int main(void)
{

    printf("%%4s\n");

}



$ 1
%4s
$
 
Old 09-26-2005, 12:22 AM   #28
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Can someone please tell me where I can learn more of the "Standard ANSI C Functions". Something that is more informitive, has more discription then other tutorials?
 
Old 09-26-2005, 12:57 AM   #29
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
here's a very good C book, includes ASCII to decimal conv table and lists+definitions of std includes eg stdio.h, strcat() etc.
 
Old 09-26-2005, 02:01 AM   #30
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by chrism01
here's a very good C book, includes ASCII to decimal conv table and lists+definitions of std includes eg stdio.h, strcat() etc.
Where's the book??

Also I was wondering how do I accept user input for a "char". I mean accept user input for just one character no more and no less. I have a program that is not working because of this. It gets up to the part where the user is supose to enter a S for single or a D for double. But then it crashes, if someone could tell me how to fix this program without including any new functions that I don't know about yet, that would be very helpful.

Code:
#include<stdio.h>


int main()

{

/*This program is supose to get the title, artist, number of tracks, price and if the CD is a single or double. The program gets up to single or double and crashes./*


    char title[41] = " ";
    char artist[41] = " ";
    int numOfTracks = 0;
    float price = 0;
    char albumSinDou = " ";

    printf("Please enter the title of the CD: ");
    scanf("%[^\n]", title);
    fflush(stdin);

    printf("\nPlease enter the artist of the CD: ");
    scanf("%[^\n]", artist);


    printf("\nPlease enter the number of tracks the CD has: ");
    scanf("%d", &numOfTracks);

    printf("\nPlease enter the price of the CD: ");
    scanf("%f", &price);

    printf("\nIs this CD a single or double, please press s for single or d for double: ");
    scanf("%c", albumSinDou);

    printf("Title: %s, Artist: %s, Tracks: %d, Price: $%.2f, %c", title, artist, numOfTracks, price, albumSinDou);
    fflush(stdin);
    getchar();





}
I would only like one charachter to be accepted into the single or double variable this is why I did not make it an array.


Thank You.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What Ascii Editors do you use? wh33t Mandriva 12 05-28-2004 02:25 AM
ASCII Matrix SeT General 1 07-17-2003 02:39 PM
ASCII help My_World Linux - General 1 04-13-2003 12:17 PM
ASCII Diagrams? baldy3105 LQ Suggestions & Feedback 7 03-28-2003 09:31 PM
ascii characters lakshman Linux - General 1 03-14-2003 11:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration