C program (string - binary to decimal conversion)

Click For Summary

Discussion Overview

The discussion revolves around a C program intended for converting a binary string to a decimal integer. Participants are addressing compilation issues, coding style, and potential improvements or alternatives in the code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a C program but reports a compilation error related to a missing compound statement at the end of the main function.
  • Another participant suggests counting the braces to identify mismatches in the code structure.
  • A different participant points out that there are three opening braces and only two closing braces, indicating a structural issue in the code.
  • Some participants discuss coding style preferences, such as declaring multiple variables on one line versus separately.
  • There is a suggestion to use `getch()` before the ending brace, with one participant arguing that it is unnecessary and highlighting that `getch()` is a nonstandard extension.
  • One participant mentions the function `strtoul()` as a potential alternative for handling string to unsigned long conversions, implying it may be relevant to the task at hand.

Areas of Agreement / Disagreement

Participants express various opinions on coding style and practices, but there is no consensus on the best approach to resolve the compilation issue or the use of specific functions. The discussion remains unresolved regarding the optimal solution for the binary to decimal conversion.

Contextual Notes

Limitations include potential misunderstandings of function usage, compiler-specific features, and the need for clarity on the expected input format for the binary string.

clook
Messages
32
Reaction score
0
here's what i have:

Code:
#include<stdio.h>
#include<string.h>
main() {
char string[32];
int sum = 0;
int a = 1;
int i;
printf("Please enter numbers:\n");
gets(string);
for (i=32;i<0;i--)
{
if(string[i] ==1)
{ sum = sum + a;
}
a = a * 2;
printf("The converted string is: %s \n", string[i]);
}

won't compile.. says "compound" statement is missing at the end.

what am i doing wrong?



Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
Count the number of left and right braces {}. You just spaced the one at the end of the main().
 
have you noticed one thing...that 1)in the program you have opened three braces and two closed braces{}, 2)why don't you declare as
int sum=o,a=1,i; 3)why don't you prefer to use getch( ); before the ending brace.
 
2)why don't you declare as
int sum=o,a=1,i;
It's a matter of style. Some prefer not to declare many things on one line, especially if there are initializers or modifiers involved.

3)why don't you prefer to use getch( ); before the ending brace.
(A) There's no reason to wait for a keypress
(B) getch is a nonstandard extension to the C language. You can only use it if you happen to be using a compiler or library that defines it. (Why not use getchar() instead?)
 
Did your teacher ever mention strtoul() ?
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K