-3

I'm making a program that prompts for and reads two characters from the keyboard, one at a time, from the user. The program will print all ASCII letters from the first to the last letter entered, inclusive. I have loop working now thanks to Eric J. Although it outputs the entire list of ASCII letters numbers and symbols before finally showing me my desired string of letters.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {  
    char charone, chartwo, curr;
    curr = charone;

    printf("Enter the first character:");
    scanf(" %c", &charone);
    printf("Enter the last character: ");
    scanf(" %c", &chartwo);

    while(curr <= chartwo) {  
        printf("Your character is %c\n", curr++);
    }  

    system("PAUSE");    
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
mdev
  • 7
  • 4
  • 2
    Show your code and *specifically* where you are stuck. Your question reads like a request to do your homework for you. – Eric J. Jan 27 '16 at 00:15
  • I'd like to tell you that people here won't help you with schools assignments. I suggest that you come here with a code or at least with some pieces of code. That way, we can help you. – Paulo Jan 27 '16 at 00:15
  • 1
    Just to clarify - people will help with school assignments, *as long as you show the work you have put in already and specifically where you are stuck*. People won't help with homework dumps. – Eric J. Jan 27 '16 at 00:16
  • I apologize, I'm new to the site and coding. I am not just looking for a hand out I just cant seem to get my auto loop to produce the additional letters. – mdev Jan 27 '16 at 00:20
  • Put it in your question, not a comment. Edit your question, paste it in the bottom. If it is already formatted with indentation it will likely be auto-formatted as code. If not, use the code formatting button `{ }` – Eric J. Jan 27 '16 at 00:20
  • 1
    To add code to your question, format it ready to copy and paste into the question's edit box. When it looks OK (in the edit box — ignore the preview at the moment), select it all and use the **`{}`** button above the edit box to indent it all as code. Now look at the preview and make sure it looks like code, and looks the way you want it to. Avoid tabs; they will cause trouble. – Jonathan Leffler Jan 27 '16 at 00:22
  • You're going to need to learn how to format your code if you expect much help here. Images of code are rather discouraged. Go into your editor and copy lines 6 to 26 to the clipboard. Edit your question, and paste that code in place of the current image link. Mark the code with your mouse. Click on the little button in the question editor that looks like this `{}` to format the marked code as text. – Eric J. Jan 27 '16 at 00:34
  • I finally got it to work, I apologize. Please don't be discouraged to help because I'm an idiot. Thanks. – mdev Jan 27 '16 at 00:39
  • 1
    We all start somewhere. No worries, glad you figured it out. – Eric J. Jan 27 '16 at 00:42
  • So what is the problem? – chux - Reinstate Monica Jan 27 '16 at 04:38

1 Answers1

0

The while keyword is all lower case. While should not compile.

If the goal is to print all characters from the first one to the last one (and nothing if the first one is later in the alphabet than the last one, consider this while loop

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

  char charone, chartwo;

  printf("Enter the first character:");
  scanf(" %c",&charone);
  printf("Enter the last character: ");
  scanf(" %c",&chartwo);

  char curr = charone;
  while(curr <= chartwo)
  {
    printf("Your character is %c\n",curr);
    curr++; // You can also do ++ in the previous line. Used 2 lines for clarity.
  }

  return 0;

}

You can also use a for loop

for (char curr = charone; curr <= chartwo; curr++)
{
    printf("Your character is %c\n",curr);
}
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • I chose to go with the while, although it did take me through the ASCII it has taken me through the entire series. I only need from the first inputted character to the last inputted character. – mdev Jan 27 '16 at 00:52
  • I just noticed that you define charone and chartwo as character arrays, not just characters. They should not be arrays. – Eric J. Jan 27 '16 at 01:03
  • That nearly worked, I got the letters I'm looking for but it still prints the entire ansii conversion list. – mdev Jan 27 '16 at 01:17
  • Put a space before %c in scanf. See http://stackoverflow.com/questions/13542055/how-to-do-scanf-for-single-char-in-c I compiled it locally and it worked fine. – Eric J. Jan 27 '16 at 01:23
  • I did that, I was still using "%s" because it was allowing to skip the whitespace. Although i did switch to " %c" it still gives me the same product as before. – mdev Jan 27 '16 at 01:28
  • This is what i get, sorry for images but I wasn't sure how to show you otherwise. it eventually ends the list and at the end it will for example my first character is a and my last is g it will print letters a-g at the end but before this displays. http://imgur.com/JS4QrZX – mdev Jan 27 '16 at 01:32
  • I edited my post to show the full source code that compiles and runs fine using MinGW on Windows. Not sure what else I can suggest. – Eric J. Jan 27 '16 at 01:34
  • I can't thank you enough. Realizing that i put my curr in with the initial char statement, instead of independent. Thank you so much! – mdev Jan 27 '16 at 01:39