Problems with C program- Not C++ and not Java

  • Context: C/C++ 
  • Thread starter Thread starter jasonsmith206
  • Start date Start date
  • Tags Tags
    C++ Java Program
Click For Summary
SUMMARY

The discussion focuses on completing a C program that reads a word and a number from user input and prints them in the format "word_number". The solution involves using the scanf function to read input into a character array and an integer variable, followed by using printf to format the output. The correct implementation is scanf("%s %d", userWord, &userNum); and printf("%s_%d\n", userWord, userNum);. Understanding pointers and arrays in C is essential for grasping this solution.

PREREQUISITES
  • Basic knowledge of C programming syntax
  • Understanding of the scanf and printf functions
  • Familiarity with character arrays in C
  • Concept of pointers and their relation to arrays
NEXT STEPS
  • Study the use of scanf for formatted input in C
  • Learn about character arrays and string manipulation in C
  • Explore the concept of pointers and their usage in C programming
  • Practice writing C programs that involve user input and formatted output
USEFUL FOR

C programmers, students learning C, and anyone looking to improve their skills in handling user input and output formatting in C programming.

jasonsmith206
Messages
10
Reaction score
0
A user types a word and a number on a single line. Read them into the provided variables. Then print: word_number. End with newline. Example output if user entered: Amy 5

Amy_5
#include <stdio.h>int main(void) {
char userWord[20] = "";
int userNum = 0;

/* Your solution goes here */

return 0;
}Was part of homework but its over a week old. Just want to know how to work it and any help would be greatly appreciated.

Thanks!
 
Technology news on Phys.org
Hi, and welcome to the forum.

One way to finish the program is
Code:
  scanf("%s %d", userWord, &userNum);
  printf("%s_%d\n", userWord, userNum);

To understand it, you need to know about pointers and their relation to arrays.
 
wow!

i did everything right on scanf but instead of printing what you've got i put (userWord and userNum to = word_number).. so close yet so far. Thank you so much for your help that one was driving me crazy!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 1 ·
Replies
1
Views
775
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
14
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
6K
Replies
89
Views
6K