Yea, just recently I enrolled into a basic computer programming class. We use Turbo Visual C++ and umm so far it went okay until we talked about do-for-while loops... Can anyone help in making a program in loops >_< I mean I tried using the goto statements but my professor doesn't want that in the statement T_T..

Basically the assignment or problem was to make a multiple choice program.. A) would display info B) would take info C) would shut it down.. I pretty much got displaying and taking and shutting it down part.. but.. When it came to the option of "Would you like to go back to the main menu" after you chose either A or B.. I got so confused >_<... I tried using switch and if statements cause I didnt' ahve a full understanding of do-for-while..

Another side assignment was the Fabonnaci code or something.. I know this uses the for statement.. supposedly the first two numbers in the statement are 1.. not counting zero and the next numbers take the previous two numbers and add them.. so the 3rd would be 2.. 4th would be 3.. 5th.. being 5.. etc.. I tried using the for statement but I'm not sure how to program such a problem..

I know it seems like too much but everywhere I look i see cout and cin when I use printf and scanf @_@, Please anyone help me.. I don't know where else to look

#include<stdio.h>
#include<conio.h>
 
char choice[2], restart;
int d,i;
 
main()
{
 
printf("Options:\n");
printf("\ta. Introduction.\n");
printf("\tb. Display the Fibonacci set.\n");
printf("\tc. EXIT the program.\n");
printf("\nChoose a letter: ");
scanf("%c", &choice[2]);
 
switch (choice[2]) {
 
case 'A':
case 'a':
printf("\n\n\tThe Fibonacci numbers are numbers of");
printf("\n\tan interesting sequence in which each");
printf("\n\tnumber is equal to the sum of the");
printf("\n\tprevious two numbers. In other words.");
printf("\n\t\tFi = (Fi-1)+(Fi-2)");
printf("\n\t\tWhere Fi refers to the ith Fibonacci number.");
printf("\n\t\tBy definintion F1 = F2 = 1");
printf("\n\t\tHence:\tF3 = F2 + F1 = 2");
printf("\n\t\t\tF4 = F3 + F2 = 3");
printf("\n\t\t\tF5 = F4 + F3 = 5"); 
break;
 
case 'B':
case 'b':
 
printf("\nPlease enter how many Fibonacci numbers to be displayed: 
");
scanf("%i",&d);
if (d = 0);
printf("Please enter higher than zero.");
if (d = 1);
printf("\n 1");
if (d = 3){
i = 3;
while (i < d) {
printf("%3i",i = (i-1) + (i-2));
i++;
}
}
 
break;
 
case 'C':
case 'c':
break;
}
getch();
}

This is what I was working on.. It looks so wrong and it's a disadvantage not having the actual program >_< And sorry this is my first post so I don't know if I posted this problem right

Recommended Answers

All 8 Replies

My tutorial on loops: http://www.daniweb.com/tutorials/tutorial1737.html

Some existing threads on the fibonacci sequence (oh the memories):
http://www.daniweb.com/techtalkforums/showthread.php?t=13448
http://www.daniweb.com/techtalkforums/showthread.php?t=28093

scanf() and printf() are from original C. They work in C++ because your c++ compiler is backwards compatible (or is the c++ language that is backwards compatible?) I'm really not sure at all. cin and cout operators are part of c++'s iostream library and don't exist in C.

Member Avatar for iamthwee

>A) would display info B) would take info C) would shut it down...

Try drawing a flow chart for this on paper. All loops, can be be reduced to pictorial flow charts. Flow charts are a must for beginners.

I'm going to assume csgal's tutorials helped on the programming.

One thing you will eventually need to do (like right now) is start formatting your code. Indent after ever { and unindent when you get to every }. Also every if statement is wrong. = is the assignment operator. == is for comparison. And remove the ; because that ends the if statment, making it useless (it executes nothing). So instead you need if (d == 1)

I know it seems like too much but everywhere I look i see cout and cin when I use printf and scanf @_@, Please anyone help me.. I don't know where else to look

You are using printf() correctly because you are writing C code, not C++. scanf() is also used correctly for the same reason, but the sooner you start using a different input form (fgets() with sscanf() for example) the sooner other headaches will go away, assuming your head already hurts. Check out Dave's tutorial for more information: http://www.daniweb.com/tutorials/tutorial45806.html
By the way, cin is the C++ equivalent of scanf(), cout is printf(). So for reading code they are interchangable.

Heya guys.. thanks for all the help.. I think I got this program right though.. I don't really have the program to test it out.. Could anyone please tell me if this program works...

#include <stdio.h>
#include <conio.h>
#include <math.h>
 
int loop;
int FiboNum, FiboDisp;
char return;
char choice;
 
main (){
 do {
  int == 0;
 
  printf("Here are your choices:\n\t");
  printf("A) Intro to the Fibonacci Sequence.\n\t");
  printf("B) Demo of the Fibonnaci Sequence. \n\t");
  printf("C) EXIT.\n\n");
  printf("Please enter a letter: ");
  scanf("%c",&choice);
 
  switch (choice);{
   case 'A':
   case 'a':{
    printf("\n\n\tThe Fibonacci numbers are numbers of");
    printf("\n\tan interesting sequence in which each");
    printf("\n\tnumber is equal to the sum of the");
    printf("\n\tprevious two numbers. In other words.");
    printf("\n\t\tFi = (Fi-1)+(Fi-2)");
    printf("\n\t\tWhere Fi refers to the ith Fibonacci number.");
    printf("\n\t\tBy definintion F1 = F2 = 1");
    printf("\n\t\tHence:\tF3 = F2 + F1 = 2");
    printf("\n\t\t\tF4 = F3 + F2 = 3");
    printf("\n\t\t\tF5 = F4 + F3 = 5");
    break;}
   case 'B':
   case 'b':{
    printf("\nPlease how many Fibonacci numbers to be displayed: ");
    scanf("%i", &FiboNum);
 
    switch (FiboNum){
     case 0:{
      printf("\nNo numbers at 0.");
      break;}
     case 1:{
      printf("\n\t1");
      break;}
     case 2:{
      printf("\n\t1  1);
      break;}
     default:{
     for (FiboDisp = 3; FiboDisp <= FiboNum, FiboDisp++);{
      FiboDisp = (FiboNum - 1) - (FiboNum -2);
      printf("%i",FiboDisp);}
      }
      }
     }
   case 'C':
   case 'c':{
    printf("Buh-bye~..");
    break;}
   default:{
    printf("Please enter a letter.");
    break;}
  if ((choice != 'C' || choice != 'c') && (choice == 'A' || choice == 'a' || choice == 'B' || choice == 'b')){
   printf("Do you want to go back to the menu?(Y/N): );
   scanf("%c",&return);}
 }while((choice == 'Y' || choice == 'y') && loop != 1);}

I sorta went crazy on the looping conditions.. but *shrug*

return is a keyword, so it cannot be the name of a variable.

char return;

You seem to be missing a variable name here.

int == 0;

And == is comparison, not assignment or initialization.

This is surely hosed.

switch ( choice );

This is missing the closing quote.

printf("\n\t1  1);

And this needs work too.

for ( FiboDisp = 3; FiboDisp <= FiboNum, FiboDisp++ );{

Using good indentation might give you a clue why this is messed up.

if ((choice != 'C' || choice != 'c') && (choice == 'A' || choice == 'a' || choice == 'B' || choice == 'b')){
   printf("Do you want to go back to the menu?(Y/N): );
   scanf("%c",&return);}
 }while((choice == 'Y' || choice == 'y') && loop != 1);}

And this needs a closing quote too.

printf("Do you want to go back to the menu?(Y/N): );

Then after that you'll run into issues regarding input and scanf .

Then after that you'll run into issues regarding input and scanf .

Can you please point out what needs work with the scanf's and all.. I can't exactly locate any problems.. like i said I dont' have the program to test it out..

BTW, thanks for pointing out those small details.. I fixed them already.. and that int == 0; was supposed to be loop == 0; .

Can you please point out what needs work with the scanf's and all.. I can't exactly locate any problems.. like i said I dont' have the program to test it out..

My 'tutorial' has already been pointed out to you.

BTW, thanks for pointing out those small details.. I fixed them already.. and that int == 0; was supposed to be loop == 0; .

And this issue has already been pointed out to you.

Can you please point out what needs work with the scanf's and all.. I can't exactly locate any problems.. like i said I dont' have the program to test it out..

You should download a compiler then. Try Borland's free compiler or Dev-C++

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.