My c++ program compiles but it skips over the getCaption function...Please Help!
ID: 3558868 • Letter: M
Question
My c++ program compiles but it skips over the getCaption function...Please Help!
#include <iostream>
#include "graph1.h"
using namespace std;
//Prototypes
void getCoords(int&x, int&y);
void getImageName(char imageName[], int max_length);
void getCaption(char caption[], int max_length);
void convertCaption(char caption[]);
void displayResults(int&x,int&y,char imageName[],char caption[]);
int main()
{
//Variable Declaration
int x=0;
int y=0;
const int max_length=255;
const int IMAGE_NAMES=50;
const int MAX_CAPTION=100;
char imageName[IMAGE_NAMES];
char caption[MAX_CAPTION];
displayGraphics();
//Call functions
getCoords(x,y);
getImageName(imageName,max_length);
getCaption(caption,max_length);
convertCaption(caption);
displayResults(x,y,imageName,caption);
return 0;
}
//Definitions of functions
void getCoords(int&x, int&y)
{
char buffer[255];
cout<<"Enter X coordinate for the image: ";
cin.getline(buffer,sizeof(buffer));
x=atoi(buffer);
cout<<"Enter Y coordinate for the image: ";
cin.getline(buffer,sizeof(buffer));
y=atoi(buffer);
}
void getImageName(char imageName[], int max_length)
{
cout<<"Enter image name: ";
cin.getline(imageName,sizeof(imageName));
}
void getCaption(char caption[], int max_length)
{
cout<<"Enter caption: ";
cin.getline(caption,sizeof(caption));
}
void convertCaption(char caption[]) //CONVERT THE CAPTION TO CAPITILIZE THE FIRST LETTER OF EACH WORD
{
int i;
int a = strlen(caption);
//To capitalise first letter of first word of string
if (isalpha(caption[0]))
{
caption[0] = toupper(caption[0]);
}
//To capitalise first letter in remaining words and also make other alphabets small case
for (i=1;i<a;i++)
{
if (isalpha(caption[i]) && caption[i-1] == ' ')
{
// only first letters of a word.
caption[i]= toupper(caption[i]);
}
else{caption[i]= tolower(caption[i]);
}
}
}
void displayResults(int&x,int&y,char imageName[],char caption[])
{
int i=0;
int obj_no=0;
obj_no=displayBMP(imageName,x,y);
gout << setPos(x,430) << caption << endg;
Sleep(2000);
}
Explanation / Answer
ur problem can be solved by replacing sizeof(imageName) with the constant value 50 .... as given below...
Replace " cin.getline(imageName,sizeof(imageName)); " with cin.getline(imageName,50);
I have tried this, it works.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.