C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 4th, 2013, 07:45 AM
lovecodecakes lovecodecakes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 lovecodecakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 47 sec
Reputation Power: 0
Unexpected string output

hey i m using code::blocks for c programming and when i run the code, it stops in the middle.
When i take help of debugger in below code it shows ..
a syntax error in expression near "if"..

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
 char s1[10];
 int i=0;
 printf("enter s1 \n");
    i=0;

 while(getchar()!='\t')
 {
     printf("entered while loop.Iteration no. %d \n",i);
     s1[i]=getchar();
     i++;
     printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
 }

  i=i-1;
  s1[i]='\0';

printf("Iteration no. %d & s1 char is: %s \n",i,s1);
printf("\n");

 for(i=0;s1[i]!='\0';i++)
 {
     printf("Entered for loop \n");
  if(((s1[i]>='a')&&(s1[i]<='z'))||((s1[i]>='A')&&(s1[i]<='Z')))
  {
      printf("Entered if condition \n");
      printf("Iteration no. %d \n",i);
      s1[i]=s1[i+1];
      printf("Iteration no. %d \n",i);
  }
 }
    puts(s1);
    return 0;
}

Reply With Quote
  #2  
Old February 4th, 2013, 08:29 AM
swapy swapy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 66 swapy Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 14 h 4 m
Reputation Power: 0
can you tell what output you want or what are you trying to do exactly?

Reply With Quote
  #3  
Old February 4th, 2013, 08:41 AM
lovecodecakes lovecodecakes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 lovecodecakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 47 sec
Reputation Power: 0
@swapy hi.just trying to replace the preceding character with the following one till \0. eg: here pots=>ots.
but there is some syntactical error.
Quote:
Originally Posted by swapy
can you tell what output you want or what are you trying to do exactly?

Reply With Quote
  #4  
Old February 4th, 2013, 09:51 AM
swapy swapy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 66 swapy Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 14 h 4 m
Reputation Power: 0
your loop condition seems somewhat wrong... what if i becomes 10 or more and i dont enter a space?

Reply With Quote
  #5  
Old February 4th, 2013, 10:06 AM
lovecodecakes lovecodecakes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 lovecodecakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 47 sec
Reputation Power: 0
a space? where? i is initialized 0 in for loop
Quote:
Originally Posted by swapy
your loop condition seems somewhat wrong... what if i becomes 10 or more and i dont enter a space?

Reply With Quote
  #6  
Old February 4th, 2013, 10:30 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,124 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 46 m 21 sec
Reputation Power: 1949
The debugger raised a syntax error? Not the compiler? That's a first for me in my decades of experience. BTW, compiling that with gcc and all warnings on compiled clean.

Is your input string what you expect it to be? Your input code looks like it's only inputting every other character:
Code:
 while(getchar()!='\t')
 {
     printf("entered while loop.Iteration no. %d \n",i);
     s1[i]=getchar();
     i++;
     printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
 }

Perhaps you were intending something like this:
Code:
    char ch;

    //  intervening code

 while((ch = getchar()) != '\t')
 {
     printf("entered while loop.Iteration no. %d \n",i);
     s1[i]=ch;
     i++;
     printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
 }

That second printf also doesn't look right, since you're displaying a character in s1 that you haven't written to yet. For example, the first character goes into s1[0], but then you increment i before echoing it out, so you are in fact "echoing" out s1[1], which, since you have not yet written anything there, contains whatever garbage is there. My recommendation is that the i++; should be moved to the end of the loop. Or replace the while with a for.

When your output doesn't look right, one of the first things to check is whether your input is right.

Last edited by dwise1_aol : February 4th, 2013 at 10:38 AM.

Reply With Quote
  #7  
Old February 4th, 2013, 10:45 AM
lovecodecakes lovecodecakes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 lovecodecakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 47 sec
Reputation Power: 0
@dwise God you are so right abt the increment i before echoing it out,damn silly mistakes.
And thanks a lot about that "Skipping" issue!
That solves everything.
And yea man im nice with the compiler cut lose sm slack man.

Quote:
Is your input string what you expect it to be? Your input code looks like it's only inputting every other character

Q: Why is I/p string I/putting every other character???

Quote:
Originally Posted by dwise1_aol
The debugger raised a syntax error? Not the compiler? That's a first for me in my decades of experience. BTW, compiling that with gcc and all warnings on compiled clean.

Is your input string what you expect it to be? Your input code looks like it's only inputting every other character:
Code:
 while(getchar()!='\t')
 {
     printf("entered while loop.Iteration no. %d \n",i);
     s1[i]=getchar();
     i++;
     printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
 }

Perhaps you were intending something like this:
Code:
    char ch;

    //  intervening code

 while((ch = getchar()) != '\t')
 {
     printf("entered while loop.Iteration no. %d \n",i);
     s1[i]=ch;
     i++;
     printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
 }

That second printf also doesn't look right, since you're displaying a character in s1 that you haven't written to yet. For example, the first character goes into s1[0], but then you increment i before echoing it out, so you are in fact "echoing" out s1[1], which, since you have not yet written anything there, contains whatever garbage is there. My recommendation is that the i++; should be moved to the end of the loop. Or replace the while with a for.

When your output doesn't look right, one of the first things to check is whether your input is right.

Reply With Quote
  #8  
Old February 4th, 2013, 10:55 AM
swapy swapy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 66 swapy Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 14 h 4 m
Reputation Power: 0
Quote:
Originally Posted by lovecodecakes
@dwise God you are so right abt the increment i before echoing it out,damn silly mistakes.
And thanks a lot about that "Skipping" issue!
That solves everything.
And yea man im nice with the compiler cut lose sm slack man.


Q: Why is I/p string I/putting every other character???


because you have used getchar()... it takes only 1 char input at a time...

Reply With Quote
  #9  
Old February 4th, 2013, 02:34 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 38 sec
Reputation Power: 1800
Quote:
Originally Posted by lovecodecakes
Q: Why is I/p string I/putting every other character???

Because your loop has two calls to getchar(), only the return value from one of which was assigned to the string, the other being discared after checking for TAB.

Reply With Quote
  #10  
Old February 4th, 2013, 02:47 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,124 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 46 m 21 sec
Reputation Power: 1949
Quote:
Originally Posted by lovecodecakes
And yea man im nice with the compiler cut lose sm slack man.

I don't know what you're talking about here.

Quote:
Originally Posted by lovecodecakes
Q: Why is I/p string I/putting every other character???

Because you're calling getchar() twice for every character you actually save as input. First you get a character to test it for being a tab. Then you get the next character to place into s1. Then you get the third character to test whether it's a tab. Then you get the fourth character place into s1 -- at this point, you have processed four characters from the input buffer and have only placed two of them into s1.

Take a look at my suggested code. In while, you getchar a character and assign it to a char variable, thus saving it for use. Then in the same while, you test that char for not being a tab; remember that the value of an assignment expression is the value being assigned! Then within the body of the while loop when you insert that character into the s1 array, you do still have that character because you had saved it in that char variable. And this is just one of a number of approaches you could have taken.

Sometimes, a very good debugging technique is to take paper and pencil in hand and play computer. Go through your program step-by-step and do everything that it tells you to do, keeping track of variable values on the paper. In your case, you should fill the input buffer with the string of characters that you intend to type in and keep track of which character each and every call to getchar() "eats" and which character will then be the next to be "eaten".

One big problem that programmers have is the same that writers have: when we read our own code we almost never see what we have actually written, but rather what we "see" what we remember intending to have written. One solution is to have somebody else have a look, much like a writer will have somebody else proofread their work. Usually that different set of eyes will see an error jump right out of them, one that we were ourselves blind to. Or that other person have questions about something that we had thought we had taken care of but hadn't yet. Another solution for when ours is the only set of eyes available is to read through our program painstakingly paying attention to each detail. One way of doing this is to play computer with pencil and paper. Another way is to sit down with somebody else and walk them through our program explaining exactly what it is doing; this forces you to think through each line enough to be able to explain it, whereupon your obvious mistake will suddenly become apparent to you.

It's a very common problem that we all have been through and continue to have to deal with.

Last edited by dwise1_aol : February 4th, 2013 at 02:54 PM.

Reply With Quote
  #11  
Old February 5th, 2013, 01:23 AM
lovecodecakes lovecodecakes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 lovecodecakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 47 sec
Reputation Power: 0
Thanks yea I edited my code & its up and running. Thanks for the advice on proofreading.Skipped on reading twice from I/p.


Quote:
Originally Posted by dwise1_aol
I don't know what you're talking about here.


Because you're calling getchar() twice for every character you actually save as input. First you get a character to test it for being a tab. Then you get the next character to place into s1. Then you get the third character to test whether it's a tab. Then you get the fourth character place into s1 -- at this point, you have processed four characters from the input buffer and have only placed two of them into s1.

Take a look at my suggested code. In while, you getchar a character and assign it to a char variable, thus saving it for use. Then in the same while, you test that char for not being a tab; remember that the value of an assignment expression is the value being assigned! Then within the body of the while loop when you insert that character into the s1 array, you do still have that character because you had saved it in that char variable. And this is just one of a number of approaches you could have taken.

Sometimes, a very good debugging technique is to take paper and pencil in hand and play computer. Go through your program step-by-step and do everything that it tells you to do, keeping track of variable values on the paper. In your case, you should fill the input buffer with the string of characters that you intend to type in and keep track of which character each and every call to getchar() "eats" and which character will then be the next to be "eaten".

One big problem that programmers have is the same that writers have: when we read our own code we almost never see what we have actually written, but rather what we "see" what we remember intending to have written. One solution is to have somebody else have a look, much like a writer will have somebody else proofread their work. Usually that different set of eyes will see an error jump right out of them, one that we were ourselves blind to. Or that other person have questions about something that we had thought we had taken care of but hadn't yet. Another solution for when ours is the only set of eyes available is to read through our program painstakingly paying attention to each detail. One way of doing this is to play computer with pencil and paper. Another way is to sit down with somebody else and walk them through our program explaining exactly what it is doing; this forces you to think through each line enough to be able to explain it, whereupon your obvious mistake will suddenly become apparent to you.

It's a very common problem that we all have been through and continue to have to deal with.
Comments on this post
clifford disagrees: Closing threads is for when the conversation becomes abusive or unconstructive. Do not close just
because you have an answer - a better answer or further insight may come along! Also do not quote
entire responses in your own unnecessarily.
dwise1_aol disagrees: with closing the thread. The "solution" you reach could be wrong, like in a recent thread where the
"solution was malloc'ing a single-char buffer for string input. If he had closed that thread, that
would never have been corrected.

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Unexpected string output

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap